mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Avoid suggesting 'is' for constant literals (#3146)
This commit is contained in:
parent
dbdfdeb0e1
commit
726adb7efc
3 changed files with 61 additions and 113 deletions
|
@ -16,21 +16,17 @@ if False == None: # E711, E712 (fix)
|
|||
if None == False: # E711, E712 (fix)
|
||||
pass
|
||||
|
||||
###
|
||||
# Unfixable errors
|
||||
###
|
||||
if "abc" == None: # E711
|
||||
pass
|
||||
if None == "abc": # E711
|
||||
pass
|
||||
if "abc" == False: # E712
|
||||
pass
|
||||
if False == "abc": # E712
|
||||
pass
|
||||
|
||||
###
|
||||
# Non-errors
|
||||
###
|
||||
if "abc" == None:
|
||||
pass
|
||||
if None == "abc":
|
||||
pass
|
||||
if "abc" == False:
|
||||
pass
|
||||
if False == "abc":
|
||||
pass
|
||||
if "def" == "abc":
|
||||
pass
|
||||
if False is None:
|
||||
|
|
|
@ -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,
|
||||
|
@ -176,9 +180,7 @@ 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);
|
||||
|
@ -186,9 +188,7 @@ pub fn literal_comparisons(
|
|||
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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
|
|
@ -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: ~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue