Skip to content
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

Avoid suggesting 'is' for constant literals #3146

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@
pass

###
# Unfixable errors
# Non-errors
###
if "abc" == None: # E711
if "abc" == None:
pass
if None == "abc": # E711
if None == "abc":
pass
if "abc" == False: # E712
if "abc" == False:
pass
if False == "abc": # E712
if False == "abc":
pass

###
# Non-errors
###
if "def" == "abc":
pass
if False is None:
Expand Down
110 changes: 53 additions & 57 deletions crates/ruff/src/rules/pycodestyle/rules/literal_comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,68 +102,72 @@ pub fn literal_comparisons(
// Check `left`.
let mut comparator = left;
let next = &comparators[0];
if check_none_comparisons
&& matches!(
comparator.node,
ExprKind::Constant {
value: Constant::None,
kind: None
}
)
{
if matches!(op, Cmpop::Eq) {
let diagnostic =
Diagnostic::new(NoneComparison(op.into()), Range::from_located(comparator));
if checker.patch(diagnostic.kind.rule()) && !helpers::is_constant_non_singleton(next) {
bad_ops.insert(0, Cmpop::Is);
}
diagnostics.push(diagnostic);
}
if matches!(op, Cmpop::NotEq) {
let diagnostic =
Diagnostic::new(NoneComparison(op.into()), Range::from_located(comparator));
if checker.patch(diagnostic.kind.rule()) && !helpers::is_constant_non_singleton(next) {
bad_ops.insert(0, Cmpop::IsNot);
}
diagnostics.push(diagnostic);
}
}

if check_true_false_comparisons {
if let ExprKind::Constant {
value: Constant::Bool(value),
kind: None,
} = comparator.node
if !helpers::is_constant_non_singleton(next) {
if check_none_comparisons
&& matches!(
comparator.node,
ExprKind::Constant {
value: Constant::None,
kind: None
}
)
{
if matches!(op, Cmpop::Eq) {
let diagnostic = Diagnostic::new(
TrueFalseComparison(value, op.into()),
Range::from_located(comparator),
);
if checker.patch(diagnostic.kind.rule())
&& !helpers::is_constant_non_singleton(next)
{
let diagnostic =
Diagnostic::new(NoneComparison(op.into()), Range::from_located(comparator));
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(0, Cmpop::Is);
}
diagnostics.push(diagnostic);
}
if matches!(op, Cmpop::NotEq) {
let diagnostic = Diagnostic::new(
TrueFalseComparison(value, op.into()),
Range::from_located(comparator),
);
if checker.patch(diagnostic.kind.rule())
&& !helpers::is_constant_non_singleton(next)
{
let diagnostic =
Diagnostic::new(NoneComparison(op.into()), Range::from_located(comparator));
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(0, Cmpop::IsNot);
}
diagnostics.push(diagnostic);
}
}

if check_true_false_comparisons {
if let ExprKind::Constant {
value: Constant::Bool(value),
kind: None,
} = comparator.node
{
if matches!(op, Cmpop::Eq) {
let diagnostic = Diagnostic::new(
TrueFalseComparison(value, op.into()),
Range::from_located(comparator),
);
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(0, Cmpop::Is);
}
diagnostics.push(diagnostic);
}
if matches!(op, Cmpop::NotEq) {
let diagnostic = Diagnostic::new(
TrueFalseComparison(value, op.into()),
Range::from_located(comparator),
);
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(0, Cmpop::IsNot);
}
diagnostics.push(diagnostic);
}
}
}
}

// Check each comparator in order.
for (idx, (op, next)) in izip!(ops, comparators).enumerate() {
if helpers::is_constant_non_singleton(comparator) {
comparator = next;
continue;
}

if check_none_comparisons
&& matches!(
next.node,
Expand All @@ -176,19 +180,15 @@ pub fn literal_comparisons(
if matches!(op, Cmpop::Eq) {
let diagnostic =
Diagnostic::new(NoneComparison(op.into()), Range::from_located(next));
if checker.patch(diagnostic.kind.rule())
&& !helpers::is_constant_non_singleton(comparator)
{
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(idx, Cmpop::Is);
}
diagnostics.push(diagnostic);
}
if matches!(op, Cmpop::NotEq) {
let diagnostic =
Diagnostic::new(NoneComparison(op.into()), Range::from_located(next));
if checker.patch(diagnostic.kind.rule())
&& !helpers::is_constant_non_singleton(comparator)
{
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(idx, Cmpop::IsNot);
}
diagnostics.push(diagnostic);
Expand All @@ -206,9 +206,7 @@ pub fn literal_comparisons(
TrueFalseComparison(value, op.into()),
Range::from_located(next),
);
if checker.patch(diagnostic.kind.rule())
&& !helpers::is_constant_non_singleton(comparator)
{
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(idx, Cmpop::Is);
}
diagnostics.push(diagnostic);
Expand All @@ -218,9 +216,7 @@ pub fn literal_comparisons(
TrueFalseComparison(value, op.into()),
Range::from_located(next),
);
if checker.patch(diagnostic.kind.rule())
&& !helpers::is_constant_non_singleton(comparator)
{
if checker.patch(diagnostic.kind.rule()) {
bad_ops.insert(idx, Cmpop::IsNot);
}
diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,48 +164,4 @@ expression: diagnostics
row: 16
column: 16
parent: ~
- kind:
NoneComparison: Eq
location:
row: 22
column: 12
end_location:
row: 22
column: 16
fix: ~
parent: ~
- kind:
NoneComparison: Eq
location:
row: 24
column: 3
end_location:
row: 24
column: 7
fix: ~
parent: ~
- kind:
TrueFalseComparison:
- false
- Eq
location:
row: 26
column: 12
end_location:
row: 26
column: 17
fix: ~
parent: ~
- kind:
TrueFalseComparison:
- false
- Eq
location:
row: 28
column: 3
end_location:
row: 28
column: 8
fix: ~
parent: ~