forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_call.rs
244 lines (233 loc) · 8.18 KB
/
logging_call.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use ruff_diagnostics::{Diagnostic, Edit, Fix};
use ruff_python_ast::{self as ast, Arguments, Constant, Expr, Keyword, Operator};
use ruff_python_semantic::analyze::logging;
use ruff_python_stdlib::logging::LoggingLevel;
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
use crate::registry::{AsRule, Rule};
use crate::rules::flake8_logging_format::violations::{
LoggingExcInfo, LoggingExtraAttrClash, LoggingFString, LoggingPercentFormat,
LoggingRedundantExcInfo, LoggingStringConcat, LoggingStringFormat, LoggingWarn,
};
/// Returns `true` if the attribute is a reserved attribute on the `logging` module's `LogRecord`
/// class.
fn is_reserved_attr(attr: &str) -> bool {
matches!(
attr,
"args"
| "asctime"
| "created"
| "exc_info"
| "exc_text"
| "filename"
| "funcName"
| "levelname"
| "levelno"
| "lineno"
| "module"
| "msecs"
| "message"
| "msg"
| "name"
| "pathname"
| "process"
| "processName"
| "relativeCreated"
| "stack_info"
| "thread"
| "threadName"
)
}
/// Check logging messages for violations.
fn check_msg(checker: &mut Checker, msg: &Expr) {
match msg {
// Check for string concatenation and percent format.
Expr::BinOp(ast::ExprBinOp { op, .. }) => match op {
Operator::Add => {
if checker.enabled(Rule::LoggingStringConcat) {
checker
.diagnostics
.push(Diagnostic::new(LoggingStringConcat, msg.range()));
}
}
Operator::Mod => {
if checker.enabled(Rule::LoggingPercentFormat) {
checker
.diagnostics
.push(Diagnostic::new(LoggingPercentFormat, msg.range()));
}
}
_ => {}
},
// Check for f-strings.
Expr::FString(_) => {
if checker.enabled(Rule::LoggingFString) {
checker
.diagnostics
.push(Diagnostic::new(LoggingFString, msg.range()));
}
}
// Check for .format() calls.
Expr::Call(ast::ExprCall { func, .. }) => {
if checker.enabled(Rule::LoggingStringFormat) {
if let Expr::Attribute(ast::ExprAttribute { value, attr, .. }) = func.as_ref() {
if attr == "format" && value.is_constant_expr() {
checker
.diagnostics
.push(Diagnostic::new(LoggingStringFormat, msg.range()));
}
}
}
}
_ => {}
}
}
/// Check contents of the `extra` argument to logging calls.
fn check_log_record_attr_clash(checker: &mut Checker, extra: &Keyword) {
match &extra.value {
Expr::Dict(ast::ExprDict { keys, .. }) => {
for key in keys {
if let Some(key) = &key {
if let Expr::Constant(ast::ExprConstant {
value: Constant::Str(attr),
..
}) = key
{
if is_reserved_attr(attr) {
checker.diagnostics.push(Diagnostic::new(
LoggingExtraAttrClash(attr.to_string()),
key.range(),
));
}
}
}
}
}
Expr::Call(ast::ExprCall {
func,
arguments: Arguments { keywords, .. },
..
}) => {
if checker
.semantic()
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.as_slice(), ["", "dict"]))
{
for keyword in keywords {
if let Some(attr) = &keyword.arg {
if is_reserved_attr(attr) {
checker.diagnostics.push(Diagnostic::new(
LoggingExtraAttrClash(attr.to_string()),
keyword.range(),
));
}
}
}
}
}
_ => {}
}
}
#[derive(Debug, Copy, Clone)]
enum LoggingCallType {
/// Logging call with a level method, e.g., `logging.info`.
LevelCall(LoggingLevel),
/// Logging call with an integer level as an argument, e.g., `logger.log(level, ...)`.
LogCall,
}
impl LoggingCallType {
fn from_attribute(attr: &str) -> Option<Self> {
if attr == "log" {
Some(LoggingCallType::LogCall)
} else {
LoggingLevel::from_attribute(attr).map(LoggingCallType::LevelCall)
}
}
}
/// Check logging calls for violations.
pub(crate) fn logging_call(checker: &mut Checker, call: &ast::ExprCall) {
// Determine the call type (e.g., `info` vs. `exception`) and the range of the attribute.
let (logging_call_type, range) = match call.func.as_ref() {
Expr::Attribute(ast::ExprAttribute { value: _, attr, .. }) => {
let Some(call_type) = LoggingCallType::from_attribute(attr.as_str()) else {
return;
};
if !logging::is_logger_candidate(
&call.func,
checker.semantic(),
&checker.settings.logger_objects,
) {
return;
}
(call_type, attr.range())
}
Expr::Name(_) => {
let Some(call_path) = checker.semantic().resolve_call_path(call.func.as_ref()) else {
return;
};
let ["logging", attribute] = call_path.as_slice() else {
return;
};
let Some(call_type) = LoggingCallType::from_attribute(attribute) else {
return;
};
(call_type, call.func.range())
}
_ => return,
};
// G001 - G004
let msg_pos = usize::from(matches!(logging_call_type, LoggingCallType::LogCall));
if let Some(format_arg) = call.arguments.find_argument("msg", msg_pos) {
check_msg(checker, format_arg);
}
// G010
if checker.enabled(Rule::LoggingWarn) {
if matches!(
logging_call_type,
LoggingCallType::LevelCall(LoggingLevel::Warn)
) {
let mut diagnostic = Diagnostic::new(LoggingWarn, range);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::automatic(Edit::range_replacement(
"warning".to_string(),
range,
)));
}
checker.diagnostics.push(diagnostic);
}
}
// G101
if checker.enabled(Rule::LoggingExtraAttrClash) {
if let Some(extra) = call.arguments.find_keyword("extra") {
check_log_record_attr_clash(checker, extra);
}
}
// G201, G202
if checker.any_enabled(&[Rule::LoggingExcInfo, Rule::LoggingRedundantExcInfo]) {
if !checker.semantic().in_exception_handler() {
return;
}
let Some(exc_info) = logging::exc_info(&call.arguments, checker.semantic()) else {
return;
};
if let LoggingCallType::LevelCall(logging_level) = logging_call_type {
match logging_level {
LoggingLevel::Error => {
if checker.enabled(Rule::LoggingExcInfo) {
checker
.diagnostics
.push(Diagnostic::new(LoggingExcInfo, range));
}
}
LoggingLevel::Exception => {
if checker.enabled(Rule::LoggingRedundantExcInfo) {
checker
.diagnostics
.push(Diagnostic::new(LoggingRedundantExcInfo, exc_info.range()));
}
}
_ => {}
}
}
}
}