-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
ruff
] Implement invalid-assert-message-literal-argument
(`RUF040…
…`) (#14488) ## Summary This PR implements new rule discussed [here](#14449). In short, it searches for assert messages which were unintentionally used as a expression to be matched against. ## Test Plan `cargo test` and review of `ruff-ecosystem`
- Loading branch information
Showing
8 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
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,5 @@ | ||
fruits = ["apples", "plums", "pear"] | ||
fruits.filter(lambda fruit: fruit.startwith("p")) | ||
assert len(fruits), 2 | ||
|
||
assert True, "always true" |
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
59 changes: 59 additions & 0 deletions
59
crates/ruff_linter/src/rules/ruff/rules/invalid_assert_message_literal_argument.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,59 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{Expr, StmtAssert}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for invalid use of literals in assert message argument. | ||
/// | ||
/// ## Why is this bad? | ||
/// An assert message which is a non-string literal was likely intended | ||
/// to be used in a comparison assertion, rather than as a message. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// fruits = ["apples", "plums", "pears"] | ||
/// fruits.filter(lambda fruit: fruit.startwith("p")) | ||
/// assert len(fruits), 2 # True unless the list is empty | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// fruits = ["apples", "plums", "pears"] | ||
/// fruits.filter(lambda fruit: fruit.startwith("p")) | ||
/// assert len(fruits) == 2 | ||
/// ``` | ||
#[violation] | ||
pub struct InvalidAssertMessageLiteralArgument; | ||
|
||
impl Violation for InvalidAssertMessageLiteralArgument { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
"Non-string literal used as assert message".to_string() | ||
} | ||
} | ||
|
||
/// RUF040 | ||
pub(crate) fn invalid_assert_message_literal_argument(checker: &mut Checker, stmt: &StmtAssert) { | ||
let Some(message) = stmt.msg.as_deref() else { | ||
return; | ||
}; | ||
|
||
if !matches!( | ||
message, | ||
Expr::NumberLiteral(_) | ||
| Expr::BooleanLiteral(_) | ||
| Expr::NoneLiteral(_) | ||
| Expr::EllipsisLiteral(_) | ||
| Expr::BytesLiteral(_) | ||
) { | ||
return; | ||
} | ||
|
||
checker.diagnostics.push(Diagnostic::new( | ||
InvalidAssertMessageLiteralArgument, | ||
message.range(), | ||
)); | ||
} |
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
13 changes: 13 additions & 0 deletions
13
...ff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF040_RUF040.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,13 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
snapshot_kind: text | ||
--- | ||
RUF040.py:3:21: RUF040 Non-string literal used as assert message | ||
| | ||
1 | fruits = ["apples", "plums", "pear"] | ||
2 | fruits.filter(lambda fruit: fruit.startwith("p")) | ||
3 | assert len(fruits), 2 | ||
| ^ RUF040 | ||
4 | | ||
5 | assert True, "always true" | ||
| |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.