-
Notifications
You must be signed in to change notification settings - Fork 883
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
crates/ruff/resources/test/fixtures/pylint/assert_on_string_literal.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,24 @@ | ||
def test_division(): | ||
a = 9 / 3 | ||
assert "No ZeroDivisionError were raised" # [assert-on-string-literal] | ||
|
||
|
||
def test_division(): | ||
a = 9 / 3 | ||
assert a == 3 | ||
|
||
|
||
try: | ||
assert "bad" # [assert-on-string-literal] | ||
except: | ||
assert "bad again" # [assert-on-string-literal] | ||
|
||
a = 12 | ||
assert f"hello {a}" # [assert-on-string-literal] | ||
assert f"{a}" # [assert-on-string-literal] | ||
assert f"" # [assert-on-string-literal] | ||
assert "" # [assert-on-string-literal] | ||
assert b"hello" # [assert-on-string-literal] | ||
assert "", b"hi" # [assert-on-string-literal] | ||
assert "WhyNotHere?", "HereIsOk" # [assert-on-string-literal] | ||
assert 12, "ok here" |
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
105 changes: 105 additions & 0 deletions
105
crates/ruff/src/rules/pylint/rules/assert_on_string_literal.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,105 @@ | ||
use rustpython_parser::ast::{Constant, Expr, ExprKind}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::types::Range; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
#[derive(Debug, PartialEq, Eq, Copy, Clone)] | ||
enum Kind { | ||
Empty, | ||
NonEmpty, | ||
Unknown, | ||
} | ||
|
||
/// ## What it does | ||
/// Checks for `assert` statements that use a string literal as the first | ||
/// argument. | ||
/// | ||
/// ## Why is this bad? | ||
/// An `assert` on a non-empty string literal will always pass, while an | ||
/// `assert` on an empty string literal will always fail. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// assert "always true" | ||
/// ``` | ||
#[violation] | ||
pub struct AssertOnStringLiteral { | ||
kind: Kind, | ||
} | ||
|
||
impl Violation for AssertOnStringLiteral { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let AssertOnStringLiteral { kind } = self; | ||
match kind { | ||
Kind::Empty => format!("Asserting on an empty string literal will never pass"), | ||
Kind::NonEmpty => format!("Asserting on a non-empty string literal will always pass"), | ||
Kind::Unknown => format!("Asserting on a string literal may have unintended results"), | ||
} | ||
} | ||
} | ||
|
||
/// PLW0129 | ||
pub fn assert_on_string_literal(checker: &mut Checker, test: &Expr) { | ||
match &test.node { | ||
ExprKind::Constant { value, .. } => match value { | ||
Constant::Str(value, ..) => { | ||
checker.diagnostics.push(Diagnostic::new( | ||
AssertOnStringLiteral { | ||
kind: if value.is_empty() { | ||
Kind::Empty | ||
} else { | ||
Kind::NonEmpty | ||
}, | ||
}, | ||
Range::from(test), | ||
)); | ||
} | ||
Constant::Bytes(value) => { | ||
checker.diagnostics.push(Diagnostic::new( | ||
AssertOnStringLiteral { | ||
kind: if value.is_empty() { | ||
Kind::Empty | ||
} else { | ||
Kind::NonEmpty | ||
}, | ||
}, | ||
Range::from(test), | ||
)); | ||
} | ||
_ => {} | ||
}, | ||
ExprKind::JoinedStr { values } => { | ||
checker.diagnostics.push(Diagnostic::new( | ||
AssertOnStringLiteral { | ||
kind: if values.iter().all(|value| match &value.node { | ||
ExprKind::Constant { value, .. } => match value { | ||
Constant::Str(value, ..) => value.is_empty(), | ||
Constant::Bytes(value) => value.is_empty(), | ||
_ => false, | ||
}, | ||
_ => false, | ||
}) { | ||
Kind::Empty | ||
} else if values.iter().any(|value| match &value.node { | ||
ExprKind::Constant { value, .. } => match value { | ||
Constant::Str(value, ..) => !value.is_empty(), | ||
Constant::Bytes(value) => !value.is_empty(), | ||
_ => false, | ||
}, | ||
_ => false, | ||
}) { | ||
Kind::NonEmpty | ||
} else { | ||
Kind::Unknown | ||
}, | ||
}, | ||
Range::from(test), | ||
)); | ||
} | ||
_ => {} | ||
} | ||
} |
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
135 changes: 135 additions & 0 deletions
135
...les/pylint/snapshots/ruff__rules__pylint__tests__PLW0129_assert_on_string_literal.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,135 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: AssertOnStringLiteral | ||
body: Asserting on a non-empty string literal will always pass | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 3 | ||
column: 11 | ||
end_location: | ||
row: 3 | ||
column: 45 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: AssertOnStringLiteral | ||
body: Asserting on a non-empty string literal will always pass | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 12 | ||
column: 11 | ||
end_location: | ||
row: 12 | ||
column: 16 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: AssertOnStringLiteral | ||
body: Asserting on a non-empty string literal will always pass | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 14 | ||
column: 11 | ||
end_location: | ||
row: 14 | ||
column: 22 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: AssertOnStringLiteral | ||
body: Asserting on a non-empty string literal will always pass | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 17 | ||
column: 7 | ||
end_location: | ||
row: 17 | ||
column: 19 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: AssertOnStringLiteral | ||
body: Asserting on a string literal may have unintended results | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 18 | ||
column: 7 | ||
end_location: | ||
row: 18 | ||
column: 13 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: AssertOnStringLiteral | ||
body: Asserting on an empty string literal will never pass | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 19 | ||
column: 7 | ||
end_location: | ||
row: 19 | ||
column: 10 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: AssertOnStringLiteral | ||
body: Asserting on an empty string literal will never pass | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 20 | ||
column: 7 | ||
end_location: | ||
row: 20 | ||
column: 9 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: AssertOnStringLiteral | ||
body: Asserting on a non-empty string literal will always pass | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 21 | ||
column: 7 | ||
end_location: | ||
row: 21 | ||
column: 15 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: AssertOnStringLiteral | ||
body: Asserting on an empty string literal will never pass | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 22 | ||
column: 7 | ||
end_location: | ||
row: 22 | ||
column: 9 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: AssertOnStringLiteral | ||
body: Asserting on a non-empty string literal will always pass | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 23 | ||
column: 7 | ||
end_location: | ||
row: 23 | ||
column: 20 | ||
fix: ~ | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.