-
Notifications
You must be signed in to change notification settings - Fork 901
/
print_empty_string.rs
161 lines (144 loc) · 5.11 KB
/
print_empty_string.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
use ruff_diagnostics::{AutofixKind, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Constant, Expr};
use ruff_python_codegen::Generator;
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
use crate::registry::AsRule;
/// ## What it does
/// Checks for `print` calls with an empty string as the only positional
/// argument.
///
/// ## Why is this bad?
/// Prefer calling `print` without any positional arguments, which is
/// equivalent and more concise.
///
/// ## Example
/// ```python
/// print("")
/// ```
///
/// Use instead:
/// ```python
/// print()
/// ```
///
/// ## References
/// - [Python documentation: `print`](https://docs.python.org/3/library/functions.html#print)
#[violation]
pub struct PrintEmptyString {
suggestion: String,
redundant_sep: bool,
}
impl Violation for PrintEmptyString {
const AUTOFIX: AutofixKind = AutofixKind::Sometimes;
#[derive_message_formats]
fn message(&self) -> String {
let PrintEmptyString {
suggestion,
redundant_sep,
} = self;
if redundant_sep == &true {
format!(
"Called `print` with an empty string and a redundant separator, use `{suggestion}` instead",
)
} else {
format!("Called `print` with an empty string, use `{suggestion}` instead",)
}
}
fn autofix_title(&self) -> Option<String> {
let PrintEmptyString { redundant_sep, .. } = self;
if redundant_sep == &true {
Some("Remove empty string positional argument and redundant separator".to_string())
} else {
Some("Remove empty string positional argument".to_string())
}
}
}
/// FURB105
pub(crate) fn print_empty_string(checker: &mut Checker, call: &ast::ExprCall) {
// Check if the call is to the builtin `print` function.
if checker
.semantic()
.resolve_call_path(&call.func)
.as_ref()
.is_some_and(|call_path| matches!(call_path.as_slice(), ["", "print"]))
{
// For performance reasons, defer assignment to until we know that we
// need to check if the separator is an empty string.
let mut sep_value_is_empty_string = false;
// If the call does not have only one positional argument, check if the
// `sep` keyword argument is an empty string; if it is not an empty
// string, don't trigger.
if call.arguments.args.len() != 1 {
sep_value_is_empty_string = call
.arguments
.find_keyword("sep")
.map_or(false, |keyword| is_const_empty_string(&keyword.value));
if !sep_value_is_empty_string {
return;
}
}
// Check if the positional arguments is are all empty strings, or if
// there are any empty strings and the `sep` keyword argument is also
// an empty string.
if call.arguments.args.iter().all(is_const_empty_string)
|| (sep_value_is_empty_string && call.arguments.args.iter().any(is_const_empty_string))
{
// Find the index of the `sep` keyword argument, if it exists.
let sep_index = call.arguments.keywords.iter().position(|keyword| {
keyword
.arg
.clone()
.is_some_and(|arg| arg.to_string() == "sep")
});
let suggestion = generate_suggestion(&call.clone(), sep_index, checker.generator());
let mut diagnostic = Diagnostic::new(
PrintEmptyString {
suggestion: suggestion.clone(),
redundant_sep: sep_index.is_some(),
},
call.range(),
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::suggested(Edit::replacement(
suggestion,
call.start(),
call.end(),
)));
}
checker.diagnostics.push(diagnostic);
}
}
}
/// Check if an expression is a constant empty string.
fn is_const_empty_string(expr: &Expr) -> bool {
matches!(
expr,
Expr::Constant(ast::ExprConstant {
value: Constant::Str(s),
..
}) if s.is_empty()
)
}
/// Generate a suggestion to replace a `print` call with `print` call with no
/// empty string positional arguments, and no `sep` keyword argument.
fn generate_suggestion(
call: &ast::ExprCall,
sep_index: Option<usize>,
generator: Generator,
) -> String {
// Clone the call so that we can mutate it.
let mut suggestion = call.clone();
// Remove all empty string positional arguments.
suggestion
.arguments
.args
.retain(|arg| !is_const_empty_string(arg));
// If there is a `sep` keyword argument, remove it too (the separator is
// not needed if there are no objects to separate) by finding its index.
if let Some(index) = sep_index {
suggestion.arguments.keywords.remove(index);
}
generator.expr(&suggestion.into())
}