Treat constant tuples as constants for yoda-conditions (#2265)

This commit is contained in:
Charlie Marsh
2023-01-27 11:25:57 -05:00
committed by GitHub
parent 84e4b7c96f
commit 64c4e4c6c7
3 changed files with 53 additions and 31 deletions

View File

@@ -8,9 +8,10 @@ use crate::registry::Diagnostic;
use crate::violations;
/// Return `true` if an [`Expr`] is a constant or a constant-like name.
fn is_constant(expr: &Expr) -> bool {
fn is_constant_like(expr: &Expr) -> bool {
match &expr.node {
ExprKind::Constant { .. } => true,
ExprKind::Tuple { elts, .. } => elts.iter().all(is_constant_like),
ExprKind::Name { id, .. } => string::is_upper(id),
_ => false,
}
@@ -35,7 +36,7 @@ pub fn yoda_conditions(
return;
}
if !is_constant(left) || is_constant(right) {
if !is_constant_like(left) || is_constant_like(right) {
return;
}

View File

@@ -23,7 +23,7 @@ expression: diagnostics
parent: ~
- kind:
YodaConditions:
suggestion: "compare == 'yoda'"
suggestion: "compare == \"yoda\""
location:
row: 3
column: 0
@@ -32,7 +32,7 @@ expression: diagnostics
column: 17
fix:
content:
- "compare == 'yoda'"
- "compare == \"yoda\""
location:
row: 3
column: 0
@@ -61,116 +61,135 @@ expression: diagnostics
parent: ~
- kind:
YodaConditions:
suggestion: "compare >= \"yoda\""
suggestion: "compare == (\"a\", \"b\")"
location:
row: 5
column: 0
end_location:
row: 5
column: 21
fix:
content:
- "compare == (\"a\", \"b\")"
location:
row: 5
column: 0
end_location:
row: 5
column: 21
parent: ~
- kind:
YodaConditions:
suggestion: "compare >= \"yoda\""
location:
row: 6
column: 0
end_location:
row: 6
column: 17
fix:
content:
- "compare >= \"yoda\""
location:
row: 5
row: 6
column: 0
end_location:
row: 5
row: 6
column: 17
parent: ~
- kind:
YodaConditions:
suggestion: "compare > 'yoda'"
suggestion: "compare > \"yoda\""
location:
row: 6
row: 7
column: 0
end_location:
row: 6
row: 7
column: 16
fix:
content:
- "compare > 'yoda'"
- "compare > \"yoda\""
location:
row: 6
row: 7
column: 0
end_location:
row: 6
row: 7
column: 16
parent: ~
- kind:
YodaConditions:
suggestion: age < 42
location:
row: 7
row: 8
column: 0
end_location:
row: 7
row: 8
column: 8
fix:
content:
- age < 42
location:
row: 7
row: 8
column: 0
end_location:
row: 7
row: 8
column: 8
parent: ~
- kind:
YodaConditions:
suggestion: age == YODA
location:
row: 8
row: 9
column: 0
end_location:
row: 8
row: 9
column: 11
fix:
content:
- age == YODA
location:
row: 8
row: 9
column: 0
end_location:
row: 8
row: 9
column: 11
parent: ~
- kind:
YodaConditions:
suggestion: age < YODA
location:
row: 9
row: 10
column: 0
end_location:
row: 9
row: 10
column: 10
fix:
content:
- age < YODA
location:
row: 9
row: 10
column: 0
end_location:
row: 9
row: 10
column: 10
parent: ~
- kind:
YodaConditions:
suggestion: age <= YODA
location:
row: 10
row: 11
column: 0
end_location:
row: 10
row: 11
column: 11
fix:
content:
- age <= YODA
location:
row: 10
row: 11
column: 0
end_location:
row: 10
row: 11
column: 11
parent: ~