-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend repeated-equality-comparison-target
to check for mixed orderings and Yoda conditions.
#6691
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a380270
Check Yodas
tjkuson e587e3b
Extend tests
tjkuson 2d04529
Extend tests even further
tjkuson 45cdbb7
Re-add >1 check
tjkuson be6f574
Run cargo fmt
charliermarsh 9f1721a
Merge branch 'main' into yoda-cond
charliermarsh 252b135
Minor refactors
charliermarsh bc65d4e
Add tests
charliermarsh bebbc20
Merge branch 'main' into yoda-cond
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,16 @@ use std::hash::BuildHasherDefault; | |
use std::ops::Deref; | ||
|
||
use itertools::{any, Itertools}; | ||
use ruff_python_ast::{BoolOp, CmpOp, Expr, ExprBoolOp, ExprCompare, Ranged}; | ||
use rustc_hash::FxHashMap; | ||
|
||
use crate::autofix::snippet::SourceCodeSnippet; | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::comparable::ComparableExpr; | ||
use ruff_python_ast::hashable::HashableExpr; | ||
use ruff_python_ast::{self as ast, BoolOp, CmpOp, Expr, Ranged}; | ||
use ruff_source_file::Locator; | ||
|
||
use crate::autofix::snippet::SourceCodeSnippet; | ||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
|
@@ -63,7 +64,10 @@ impl Violation for RepeatedEqualityComparisonTarget { | |
} | ||
|
||
/// PLR1714 | ||
pub(crate) fn repeated_equality_comparison_target(checker: &mut Checker, bool_op: &ExprBoolOp) { | ||
pub(crate) fn repeated_equality_comparison_target( | ||
checker: &mut Checker, | ||
bool_op: &ast::ExprBoolOp, | ||
) { | ||
if bool_op | ||
.values | ||
.iter() | ||
|
@@ -72,27 +76,45 @@ pub(crate) fn repeated_equality_comparison_target(checker: &mut Checker, bool_op | |
return; | ||
} | ||
|
||
let mut left_to_comparators: FxHashMap<HashableExpr, (usize, Vec<&Expr>)> = | ||
FxHashMap::with_capacity_and_hasher(bool_op.values.len(), BuildHasherDefault::default()); | ||
let mut value_to_comparators: FxHashMap<HashableExpr, (usize, Vec<&Expr>)> = | ||
FxHashMap::with_capacity_and_hasher( | ||
bool_op.values.len() * 2, | ||
BuildHasherDefault::default(), | ||
); | ||
|
||
for value in &bool_op.values { | ||
if let Expr::Compare(ExprCompare { | ||
// Enforced via `is_allowed_value`. | ||
let Expr::Compare(ast::ExprCompare { | ||
left, comparators, .. | ||
}) = value | ||
{ | ||
let (count, matches) = left_to_comparators | ||
.entry(left.deref().into()) | ||
.or_insert_with(|| (0, Vec::new())); | ||
*count += 1; | ||
matches.extend(comparators); | ||
} | ||
else { | ||
return; | ||
}; | ||
|
||
// Enforced via `is_allowed_value`. | ||
let [right] = comparators.as_slice() else { | ||
return; | ||
}; | ||
|
||
let (left_count, left_matches) = value_to_comparators | ||
.entry(left.deref().into()) | ||
.or_insert_with(|| (0, Vec::new())); | ||
*left_count += 1; | ||
left_matches.push(right); | ||
|
||
let (right_count, right_matches) = value_to_comparators | ||
.entry(right.into()) | ||
.or_insert_with(|| (0, Vec::new())); | ||
*right_count += 1; | ||
right_matches.push(left); | ||
} | ||
|
||
for (left, (count, comparators)) in left_to_comparators { | ||
for (value, (count, comparators)) in value_to_comparators { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tjkuson - What do you think of this? It's a bit closer to the previous version: we just track the comparators with the thing they're compared against (and this time, including the RHS), then raise if any comparator is used multiple times. |
||
if count > 1 { | ||
checker.diagnostics.push(Diagnostic::new( | ||
RepeatedEqualityComparisonTarget { | ||
expression: SourceCodeSnippet::new(merged_membership_test( | ||
left.as_expr(), | ||
value.as_expr(), | ||
bool_op.op, | ||
&comparators, | ||
checker.locator(), | ||
|
@@ -108,7 +130,7 @@ pub(crate) fn repeated_equality_comparison_target(checker: &mut Checker, bool_op | |
/// E.g., `==` operators can be joined with `or` and `!=` operators can be | ||
/// joined with `and`. | ||
fn is_allowed_value(bool_op: BoolOp, value: &Expr) -> bool { | ||
let Expr::Compare(ExprCompare { | ||
let Expr::Compare(ast::ExprCompare { | ||
left, | ||
ops, | ||
comparators, | ||
|
@@ -130,6 +152,14 @@ fn is_allowed_value(bool_op: BoolOp, value: &Expr) -> bool { | |
return false; | ||
} | ||
|
||
// Ignore self-comparisons, e.g., `foo == foo`. | ||
let [right] = comparators.as_slice() else { | ||
return false; | ||
}; | ||
if ComparableExpr::from(left) == ComparableExpr::from(right) { | ||
return false; | ||
} | ||
|
||
if left.is_call_expr() { | ||
return false; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have odd behavior for this kind of thing on
main
already:It's not awful because it's not like we autofix this and remove the comparisons, but it'd be nice to have a more targeted range. Anyway...