[Internal] Use more report_diagnostic_if_enabled#18924
[Internal] Use more report_diagnostic_if_enabled#18924ntBre merged 11 commits intoastral-sh:mainfrom
report_diagnostic_if_enabled#18924Conversation
|
ntBre
left a comment
There was a problem hiding this comment.
This looks great, thank you! I just had a couple of small suggestions on the docs.
Also, I probably should have mentioned this earlier (sorry!), and it looks like your script did a good job, but this is a nice use case for ast-grep. I just started using it recently, and it's really handy. Something like this would probably work too:
ast-grep \
-p 'if checker.is_rule_enabled($RULE) { checker.report_diagnostic($KIND, $RANGE); }' \
-r 'checker.report_diagnostic_if_enabled($KIND, $RANGE);'It only found two additional cases, and one of them I had to clean up manually since it was in an else-if.
Diff
diff --git a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs
index 21e8aa0d11..8b094b346f 100644
--- a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs
+++ b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs
@@ -1311,16 +1311,12 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
typ: CFormatErrorType::UnsupportedFormatChar(c),
..
}) => {
- if checker
- .is_rule_enabled(Rule::PercentFormatUnsupportedFormatCharacter)
- {
- checker.report_diagnostic(
- pyflakes::rules::PercentFormatUnsupportedFormatCharacter {
- char: c,
- },
- location,
- );
- }
+ checker.report_diagnostic_if_enabled(
+ pyflakes::rules::PercentFormatUnsupportedFormatCharacter {
+ char: c,
+ },
+ location,
+ );
}
Err(e) => {
checker.report_diagnostic_if_enabled(
diff --git a/crates/ruff_linter/src/rules/flake8_2020/rules/compare.rs b/crates/ruff_linter/src/rules/flake8_2020/rules/compare.rs
index 01751361da..12c18a2dd7 100644
--- a/crates/ruff_linter/src/rules/flake8_2020/rules/compare.rs
+++ b/crates/ruff_linter/src/rules/flake8_2020/rules/compare.rs
@@ -307,8 +307,8 @@ pub(crate) fn compare(checker: &Checker, left: &Expr, ops: &[CmpOp], comparators
{
if value.len() == 1 {
checker.report_diagnostic_if_enabled(SysVersionCmpStr10, left.range());
- } else if checker.is_rule_enabled(Rule::SysVersionCmpStr3) {
- checker.report_diagnostic(SysVersionCmpStr3, left.range());
+ } else {
+ checker.report_diagnostic_if_enabled(SysVersionCmpStr3, left.range());
}
}
}|
Docs updated to fix the inconsistencies, and I updated the script to handle those cases. |
ntBre
left a comment
There was a problem hiding this comment.
Nice, thank you!
I think I should have merged this before your rule code changes 😬 I hope the merge conflicts aren't too much of a pain. I'm happy to resolve them if you want.
|
I'm surprised there were any merge conflicts, I thought git would have been smarter. There weren't too many and they were all easy to resolve (assuming/hoping I did it right) |
Summary
From @ntBre #18906 (comment) :
This PR folds those calls. I also updated the doc comments by copying from
report_diagnostic.Note: It seems odd to me that the doc comment for
CheckersaysDiagnosticwhileLintContextsaysOldDiagnostic, not sure if that needs a bigger docs change to fix the inconsistency.Python script to do the changes
This script assumes it is placed in the top level
ruffdirectory (ie next to.git/crates/README.md)Test Plan
N/A, no tests/functionality affected.