Skip to content

Commit ca497fa

Browse files
Remove some diagnostics.extend calls (#5483)
## Summary It's more efficient (and more idiomatic for us) to pass in the `Checker` directly.
1 parent 00fbbe4 commit ca497fa

File tree

10 files changed

+75
-90
lines changed

10 files changed

+75
-90
lines changed

crates/ruff/src/checkers/ast/mod.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,7 @@ where
359359
..
360360
}) => {
361361
if self.enabled(Rule::DjangoNonLeadingReceiverDecorator) {
362-
self.diagnostics
363-
.extend(flake8_django::rules::non_leading_receiver_decorator(
364-
decorator_list,
365-
|expr| self.semantic.resolve_call_path(expr),
366-
));
362+
flake8_django::rules::non_leading_receiver_decorator(self, decorator_list);
367363
}
368364
if self.enabled(Rule::AmbiguousFunctionName) {
369365
if let Some(diagnostic) =
@@ -505,8 +501,7 @@ where
505501
}
506502
}
507503
if self.enabled(Rule::HardcodedPasswordDefault) {
508-
self.diagnostics
509-
.extend(flake8_bandit::rules::hardcoded_password_default(args));
504+
flake8_bandit::rules::hardcoded_password_default(self, args);
510505
}
511506
if self.enabled(Rule::PropertyWithParameters) {
512507
pylint::rules::property_with_parameters(self, stmt, decorator_list, args);
@@ -1573,9 +1568,7 @@ where
15731568
pyupgrade::rules::os_error_alias_handlers(self, handlers);
15741569
}
15751570
if self.enabled(Rule::PytestAssertInExcept) {
1576-
self.diagnostics.extend(
1577-
flake8_pytest_style::rules::assert_in_exception_handler(handlers),
1578-
);
1571+
flake8_pytest_style::rules::assert_in_exception_handler(self, handlers);
15791572
}
15801573
if self.enabled(Rule::SuppressibleException) {
15811574
flake8_simplify::rules::suppressible_exception(
@@ -1616,11 +1609,7 @@ where
16161609
flake8_bugbear::rules::assignment_to_os_environ(self, targets);
16171610
}
16181611
if self.enabled(Rule::HardcodedPasswordString) {
1619-
if let Some(diagnostic) =
1620-
flake8_bandit::rules::assign_hardcoded_password_string(value, targets)
1621-
{
1622-
self.diagnostics.push(diagnostic);
1623-
}
1612+
flake8_bandit::rules::assign_hardcoded_password_string(self, value, targets);
16241613
}
16251614
if self.enabled(Rule::GlobalStatement) {
16261615
for target in targets.iter() {
@@ -2615,8 +2604,7 @@ where
26152604
flake8_bandit::rules::jinja2_autoescape_false(self, func, args, keywords);
26162605
}
26172606
if self.enabled(Rule::HardcodedPasswordFuncArg) {
2618-
self.diagnostics
2619-
.extend(flake8_bandit::rules::hardcoded_password_func_arg(keywords));
2607+
flake8_bandit::rules::hardcoded_password_func_arg(self, keywords);
26202608
}
26212609
if self.enabled(Rule::HardcodedSQLExpression) {
26222610
flake8_bandit::rules::hardcoded_sql_expression(self, expr);
@@ -2871,16 +2859,13 @@ where
28712859
&self.settings.flake8_gettext.functions_names,
28722860
) {
28732861
if self.enabled(Rule::FStringInGetTextFuncCall) {
2874-
self.diagnostics
2875-
.extend(flake8_gettext::rules::f_string_in_gettext_func_call(args));
2862+
flake8_gettext::rules::f_string_in_gettext_func_call(self, args);
28762863
}
28772864
if self.enabled(Rule::FormatInGetTextFuncCall) {
2878-
self.diagnostics
2879-
.extend(flake8_gettext::rules::format_in_gettext_func_call(args));
2865+
flake8_gettext::rules::format_in_gettext_func_call(self, args);
28802866
}
28812867
if self.enabled(Rule::PrintfInGetTextFuncCall) {
2882-
self.diagnostics
2883-
.extend(flake8_gettext::rules::printf_in_gettext_func_call(args));
2868+
flake8_gettext::rules::printf_in_gettext_func_call(self, args);
28842869
}
28852870
}
28862871
if self.enabled(Rule::UncapitalizedEnvironmentVariables) {
@@ -3221,11 +3206,10 @@ where
32213206
flake8_2020::rules::compare(self, left, ops, comparators);
32223207
}
32233208
if self.enabled(Rule::HardcodedPasswordString) {
3224-
self.diagnostics.extend(
3225-
flake8_bandit::rules::compare_to_hardcoded_password_string(
3226-
left,
3227-
comparators,
3228-
),
3209+
flake8_bandit::rules::compare_to_hardcoded_password_string(
3210+
self,
3211+
left,
3212+
comparators,
32293213
);
32303214
}
32313215
if self.enabled(Rule::ComparisonWithItself) {

crates/ruff/src/rules/flake8_bandit/rules/hardcoded_password_default.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustpython_parser::ast::{Arg, ArgWithDefault, Arguments, Expr, Ranged};
22

3+
use crate::checkers::ast::Checker;
34
use ruff_diagnostics::{Diagnostic, Violation};
45
use ruff_macros::{derive_message_formats, violation};
56

@@ -36,9 +37,7 @@ fn check_password_kwarg(arg: &Arg, default: &Expr) -> Option<Diagnostic> {
3637
}
3738

3839
/// S107
39-
pub(crate) fn hardcoded_password_default(arguments: &Arguments) -> Vec<Diagnostic> {
40-
let mut diagnostics: Vec<Diagnostic> = Vec::new();
41-
40+
pub(crate) fn hardcoded_password_default(checker: &mut Checker, arguments: &Arguments) {
4241
for ArgWithDefault {
4342
def,
4443
default,
@@ -53,9 +52,7 @@ pub(crate) fn hardcoded_password_default(arguments: &Arguments) -> Vec<Diagnosti
5352
continue;
5453
};
5554
if let Some(diagnostic) = check_password_kwarg(def, default) {
56-
diagnostics.push(diagnostic);
55+
checker.diagnostics.push(diagnostic);
5756
}
5857
}
59-
60-
diagnostics
6158
}

crates/ruff/src/rules/flake8_bandit/rules/hardcoded_password_func_arg.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustpython_parser::ast::{Keyword, Ranged};
22

3+
use crate::checkers::ast::Checker;
34
use ruff_diagnostics::{Diagnostic, Violation};
45
use ruff_macros::{derive_message_formats, violation};
56

@@ -22,10 +23,10 @@ impl Violation for HardcodedPasswordFuncArg {
2223
}
2324

2425
/// S106
25-
pub(crate) fn hardcoded_password_func_arg(keywords: &[Keyword]) -> Vec<Diagnostic> {
26-
keywords
27-
.iter()
28-
.filter_map(|keyword| {
26+
pub(crate) fn hardcoded_password_func_arg(checker: &mut Checker, keywords: &[Keyword]) {
27+
checker
28+
.diagnostics
29+
.extend(keywords.iter().filter_map(|keyword| {
2930
string_literal(&keyword.value).filter(|string| !string.is_empty())?;
3031
let arg = keyword.arg.as_ref()?;
3132
if !matches_password_name(arg) {
@@ -37,6 +38,5 @@ pub(crate) fn hardcoded_password_func_arg(keywords: &[Keyword]) -> Vec<Diagnosti
3738
},
3839
keyword.range(),
3940
))
40-
})
41-
.collect()
41+
}));
4242
}

crates/ruff/src/rules/flake8_bandit/rules/hardcoded_password_string.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use rustpython_parser::ast::{self, Constant, Expr, Ranged};
33
use ruff_diagnostics::{Diagnostic, Violation};
44
use ruff_macros::{derive_message_formats, violation};
55

6+
use crate::checkers::ast::Checker;
7+
68
use super::super::helpers::{matches_password_name, string_literal};
79

810
#[violation]
@@ -47,12 +49,13 @@ fn password_target(target: &Expr) -> Option<&str> {
4749

4850
/// S105
4951
pub(crate) fn compare_to_hardcoded_password_string(
52+
checker: &mut Checker,
5053
left: &Expr,
5154
comparators: &[Expr],
52-
) -> Vec<Diagnostic> {
53-
comparators
54-
.iter()
55-
.filter_map(|comp| {
55+
) {
56+
checker
57+
.diagnostics
58+
.extend(comparators.iter().filter_map(|comp| {
5659
string_literal(comp).filter(|string| !string.is_empty())?;
5760
let Some(name) = password_target(left) else {
5861
return None;
@@ -63,29 +66,29 @@ pub(crate) fn compare_to_hardcoded_password_string(
6366
},
6467
comp.range(),
6568
))
66-
})
67-
.collect()
69+
}));
6870
}
6971

7072
/// S105
7173
pub(crate) fn assign_hardcoded_password_string(
74+
checker: &mut Checker,
7275
value: &Expr,
7376
targets: &[Expr],
74-
) -> Option<Diagnostic> {
77+
) {
7578
if string_literal(value)
7679
.filter(|string| !string.is_empty())
7780
.is_some()
7881
{
7982
for target in targets {
8083
if let Some(name) = password_target(target) {
81-
return Some(Diagnostic::new(
84+
checker.diagnostics.push(Diagnostic::new(
8285
HardcodedPasswordString {
8386
name: name.to_string(),
8487
},
8588
value.range(),
8689
));
90+
return;
8791
}
8892
}
8993
}
90-
None
9194
}

crates/ruff/src/rules/flake8_django/rules/non_leading_receiver_decorator.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use rustpython_parser::ast::{self, Decorator, Expr, Ranged};
1+
use rustpython_parser::ast::{Decorator, Ranged};
22

33
use ruff_diagnostics::{Diagnostic, Violation};
44
use ruff_macros::{derive_message_formats, violation};
5-
use ruff_python_ast::call_path::CallPath;
5+
6+
use crate::checkers::ast::Checker;
67

78
/// ## What it does
89
/// Checks that Django's `@receiver` decorator is listed first, prior to
@@ -48,25 +49,19 @@ impl Violation for DjangoNonLeadingReceiverDecorator {
4849
}
4950

5051
/// DJ013
51-
pub(crate) fn non_leading_receiver_decorator<'a, F>(
52-
decorator_list: &'a [Decorator],
53-
resolve_call_path: F,
54-
) -> Vec<Diagnostic>
55-
where
56-
F: Fn(&'a Expr) -> Option<CallPath<'a>>,
57-
{
58-
let mut diagnostics = vec![];
52+
pub(crate) fn non_leading_receiver_decorator(checker: &mut Checker, decorator_list: &[Decorator]) {
5953
let mut seen_receiver = false;
6054
for (i, decorator) in decorator_list.iter().enumerate() {
61-
let is_receiver = match &decorator.expression {
62-
Expr::Call(ast::ExprCall { func, .. }) => resolve_call_path(func)
55+
let is_receiver = decorator.expression.as_call_expr().map_or(false, |call| {
56+
checker
57+
.semantic()
58+
.resolve_call_path(&call.func)
6359
.map_or(false, |call_path| {
6460
matches!(call_path.as_slice(), ["django", "dispatch", "receiver"])
65-
}),
66-
_ => false,
67-
};
61+
})
62+
});
6863
if i > 0 && is_receiver && !seen_receiver {
69-
diagnostics.push(Diagnostic::new(
64+
checker.diagnostics.push(Diagnostic::new(
7065
DjangoNonLeadingReceiverDecorator,
7166
decorator.range(),
7267
));
@@ -77,5 +72,4 @@ where
7772
seen_receiver = true;
7873
}
7974
}
80-
diagnostics
8175
}

crates/ruff/src/rules/flake8_gettext/rules/f_string_in_gettext_func_call.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use rustpython_parser::ast::{Expr, Ranged};
33
use ruff_diagnostics::{Diagnostic, Violation};
44
use ruff_macros::{derive_message_formats, violation};
55

6+
use crate::checkers::ast::Checker;
7+
68
#[violation]
79
pub struct FStringInGetTextFuncCall;
810

@@ -14,11 +16,12 @@ impl Violation for FStringInGetTextFuncCall {
1416
}
1517

1618
/// INT001
17-
pub(crate) fn f_string_in_gettext_func_call(args: &[Expr]) -> Option<Diagnostic> {
19+
pub(crate) fn f_string_in_gettext_func_call(checker: &mut Checker, args: &[Expr]) {
1820
if let Some(first) = args.first() {
1921
if first.is_joined_str_expr() {
20-
return Some(Diagnostic::new(FStringInGetTextFuncCall {}, first.range()));
22+
checker
23+
.diagnostics
24+
.push(Diagnostic::new(FStringInGetTextFuncCall {}, first.range()));
2125
}
2226
}
23-
None
2427
}

crates/ruff/src/rules/flake8_gettext/rules/format_in_gettext_func_call.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use rustpython_parser::ast::{self, Expr, Ranged};
33
use ruff_diagnostics::{Diagnostic, Violation};
44
use ruff_macros::{derive_message_formats, violation};
55

6+
use crate::checkers::ast::Checker;
7+
68
#[violation]
79
pub struct FormatInGetTextFuncCall;
810

@@ -14,15 +16,16 @@ impl Violation for FormatInGetTextFuncCall {
1416
}
1517

1618
/// INT002
17-
pub(crate) fn format_in_gettext_func_call(args: &[Expr]) -> Option<Diagnostic> {
19+
pub(crate) fn format_in_gettext_func_call(checker: &mut Checker, args: &[Expr]) {
1820
if let Some(first) = args.first() {
1921
if let Expr::Call(ast::ExprCall { func, .. }) = &first {
2022
if let Expr::Attribute(ast::ExprAttribute { attr, .. }) = func.as_ref() {
2123
if attr == "format" {
22-
return Some(Diagnostic::new(FormatInGetTextFuncCall {}, first.range()));
24+
checker
25+
.diagnostics
26+
.push(Diagnostic::new(FormatInGetTextFuncCall {}, first.range()));
2327
}
2428
}
2529
}
2630
}
27-
None
2831
}

crates/ruff/src/rules/flake8_gettext/rules/printf_in_gettext_func_call.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustpython_parser::ast::{self, Constant, Expr, Operator, Ranged};
22

3+
use crate::checkers::ast::Checker;
34
use ruff_diagnostics::{Diagnostic, Violation};
45
use ruff_macros::{derive_message_formats, violation};
56

@@ -14,7 +15,7 @@ impl Violation for PrintfInGetTextFuncCall {
1415
}
1516

1617
/// INT003
17-
pub(crate) fn printf_in_gettext_func_call(args: &[Expr]) -> Option<Diagnostic> {
18+
pub(crate) fn printf_in_gettext_func_call(checker: &mut Checker, args: &[Expr]) {
1819
if let Some(first) = args.first() {
1920
if let Expr::BinOp(ast::ExprBinOp {
2021
op: Operator::Mod { .. },
@@ -27,9 +28,10 @@ pub(crate) fn printf_in_gettext_func_call(args: &[Expr]) -> Option<Diagnostic> {
2728
..
2829
}) = left.as_ref()
2930
{
30-
return Some(Diagnostic::new(PrintfInGetTextFuncCall {}, first.range()));
31+
checker
32+
.diagnostics
33+
.push(Diagnostic::new(PrintfInGetTextFuncCall {}, first.range()));
3134
}
3235
}
3336
}
34-
None
3537
}

crates/ruff/src/rules/flake8_pytest_style/rules/assertion.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ pub(crate) fn assert_falsy(checker: &mut Checker, stmt: &Stmt, test: &Expr) {
226226
}
227227

228228
/// PT017
229-
pub(crate) fn assert_in_exception_handler(handlers: &[ExceptHandler]) -> Vec<Diagnostic> {
230-
handlers
231-
.iter()
232-
.flat_map(|handler| match handler {
229+
pub(crate) fn assert_in_exception_handler(checker: &mut Checker, handlers: &[ExceptHandler]) {
230+
checker
231+
.diagnostics
232+
.extend(handlers.iter().flat_map(|handler| match handler {
233233
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
234234
name, body, ..
235235
}) => {
@@ -239,8 +239,7 @@ pub(crate) fn assert_in_exception_handler(handlers: &[ExceptHandler]) -> Vec<Dia
239239
Vec::new()
240240
}
241241
}
242-
})
243-
.collect()
242+
}));
244243
}
245244

246245
#[derive(Copy, Clone)]

0 commit comments

Comments
 (0)