-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
pylint
] C1901: compare-to-empty-string (#3405)
- Loading branch information
Showing
9 changed files
with
239 additions
and
12 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
crates/ruff/resources/test/fixtures/pylint/compare_to_empty_string.py
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
x = "a string" | ||
y = "another string" | ||
z = "" | ||
|
||
|
||
def errors(): | ||
if x is "" or x == "": | ||
print("x is an empty string") | ||
|
||
if y is not "" or y != "": | ||
print("y is not an empty string") | ||
|
||
if "" != z: | ||
print("z is an empty string") | ||
|
||
|
||
def ok(): | ||
if x and not y: | ||
print("x is not an empty string, but y is an empty string") |
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
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
125 changes: 125 additions & 0 deletions
125
crates/ruff/src/rules/pylint/rules/compare_to_empty_string.rs
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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use anyhow::bail; | ||
use itertools::Itertools; | ||
use rustpython_parser::ast::{Cmpop, Constant, Expr, ExprKind}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::{unparse_constant, unparse_expr}; | ||
use ruff_python_ast::types::Range; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
#[derive(Debug, PartialEq, Eq, Copy, Clone)] | ||
pub enum EmptyStringCmpop { | ||
Is, | ||
IsNot, | ||
Eq, | ||
NotEq, | ||
} | ||
|
||
impl TryFrom<&Cmpop> for EmptyStringCmpop { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(value: &Cmpop) -> Result<Self, Self::Error> { | ||
match value { | ||
Cmpop::Is => Ok(Self::Is), | ||
Cmpop::IsNot => Ok(Self::IsNot), | ||
Cmpop::Eq => Ok(Self::Eq), | ||
Cmpop::NotEq => Ok(Self::NotEq), | ||
_ => bail!("{value:?} cannot be converted to EmptyStringCmpop"), | ||
} | ||
} | ||
} | ||
|
||
impl EmptyStringCmpop { | ||
pub fn into_unary(self) -> &'static str { | ||
match self { | ||
Self::Is | Self::Eq => "", | ||
Self::IsNot | Self::NotEq => "not ", | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for EmptyStringCmpop { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let repr = match self { | ||
Self::Is => "is", | ||
Self::IsNot => "is not", | ||
Self::Eq => "==", | ||
Self::NotEq => "!=", | ||
}; | ||
write!(f, "{repr}") | ||
} | ||
} | ||
|
||
#[violation] | ||
pub struct CompareToEmptyString { | ||
pub existing: String, | ||
pub replacement: String, | ||
} | ||
|
||
impl Violation for CompareToEmptyString { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!( | ||
"`{}` can be simplified to `{}` as an empty string is falsey", | ||
self.existing, self.replacement, | ||
) | ||
} | ||
} | ||
|
||
pub fn compare_to_empty_string( | ||
checker: &mut Checker, | ||
left: &Expr, | ||
ops: &[Cmpop], | ||
comparators: &[Expr], | ||
) { | ||
let mut first = true; | ||
for ((lhs, rhs), op) in std::iter::once(left) | ||
.chain(comparators.iter()) | ||
.tuple_windows::<(&Expr<_>, &Expr<_>)>() | ||
.zip(ops) | ||
{ | ||
if let Ok(op) = EmptyStringCmpop::try_from(op) { | ||
if std::mem::take(&mut first) { | ||
// Check the left-most expression. | ||
if let ExprKind::Constant { value, .. } = &lhs.node { | ||
if let Constant::Str(s) = value { | ||
if s.is_empty() { | ||
let constant = unparse_constant(value, checker.stylist); | ||
let expr = unparse_expr(rhs, checker.stylist); | ||
let existing = format!("{constant} {op} {expr}"); | ||
let replacement = format!("{}{expr}", op.into_unary()); | ||
checker.diagnostics.push(Diagnostic::new( | ||
CompareToEmptyString { | ||
existing, | ||
replacement, | ||
}, | ||
Range::from(lhs), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Check all right-hand expressions. | ||
if let ExprKind::Constant { value, .. } = &rhs.node { | ||
if let Constant::Str(s) = value { | ||
if s.is_empty() { | ||
let expr = unparse_expr(lhs, checker.stylist); | ||
let constant = unparse_constant(value, checker.stylist); | ||
let existing = format!("{expr} {op} {constant}"); | ||
let replacement = format!("{}{expr}", op.into_unary()); | ||
checker.diagnostics.push(Diagnostic::new( | ||
CompareToEmptyString { | ||
existing, | ||
replacement, | ||
}, | ||
Range::from(rhs), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
...ules/pylint/snapshots/ruff__rules__pylint__tests__PLC1901_compare_to_empty_string.py.snap
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: CompareToEmptyString | ||
body: "`x is \"\"` can be simplified to `x` as an empty string is falsey" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 7 | ||
column: 12 | ||
end_location: | ||
row: 7 | ||
column: 14 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: CompareToEmptyString | ||
body: "`x == \"\"` can be simplified to `x` as an empty string is falsey" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 7 | ||
column: 23 | ||
end_location: | ||
row: 7 | ||
column: 25 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: CompareToEmptyString | ||
body: "`y is not \"\"` can be simplified to `not y` as an empty string is falsey" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 10 | ||
column: 16 | ||
end_location: | ||
row: 10 | ||
column: 18 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: CompareToEmptyString | ||
body: "`y != \"\"` can be simplified to `not y` as an empty string is falsey" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 10 | ||
column: 27 | ||
end_location: | ||
row: 10 | ||
column: 29 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: CompareToEmptyString | ||
body: "`\"\" != z` can be simplified to `not z` as an empty string is falsey" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 13 | ||
column: 7 | ||
end_location: | ||
row: 13 | ||
column: 9 | ||
fix: ~ | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.