Skip to content

Commit

Permalink
Flag B018 for strings and f-strings which aren't docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed May 7, 2024
1 parent 43c3e98 commit f7ee407
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Foo2:
"""abc"""

a = 2
"str" # Str (no raise)
f"{int}" # JoinedStr (no raise)
"str" # StringLiteral
f"{int}" # FString
1j # Number (complex)
1 # Number (int)
1.0 # Number (float)
Expand All @@ -34,8 +34,8 @@ def foo1():
def foo2():
"""my docstring"""
a = 2
"str" # Str (no raise)
f"{int}" # JoinedStr (no raise)
"str" # StringLiteral
f"{int}" # FString
1j # Number (complex)
1 # Number (int)
1.0 # Number (float)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,11 @@ impl Violation for UselessExpression {
/// B018
pub(crate) fn useless_expression(checker: &mut Checker, value: &Expr) {
// Ignore comparisons, as they're handled by `useless_comparison`.
if value.is_compare_expr() {
if matches!(value, Expr::Compare(_) | Expr::EllipsisLiteral(_)) {
return;
}

// Ignore strings, to avoid false positives with docstrings.
if matches!(
value,
Expr::FString(_) | Expr::StringLiteral(_) | Expr::EllipsisLiteral(_)
) {
if checker.semantic().in_docstring() || checker.semantic().in_attribute_docstring() {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
---
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
---
B018.py:10:5: B018 Found useless expression. Either assign it to a variable or remove it.
|
8 | a = 2
9 | "str" # StringLiteral
10 | f"{int}" # FString
| ^^^^^^^^ B018
11 | 1j # Number (complex)
12 | 1 # Number (int)
|

B018.py:11:5: B018 Found useless expression. Either assign it to a variable or remove it.
|
9 | "str" # Str (no raise)
10 | f"{int}" # JoinedStr (no raise)
9 | "str" # StringLiteral
10 | f"{int}" # FString
11 | 1j # Number (complex)
| ^^ B018
12 | 1 # Number (int)
Expand All @@ -13,7 +23,7 @@ B018.py:11:5: B018 Found useless expression. Either assign it to a variable or r

B018.py:12:5: B018 Found useless expression. Either assign it to a variable or remove it.
|
10 | f"{int}" # JoinedStr (no raise)
10 | f"{int}" # FString
11 | 1j # Number (complex)
12 | 1 # Number (int)
| ^ B018
Expand Down Expand Up @@ -115,10 +125,30 @@ B018.py:27:5: B018 Found useless expression. Either assign it to a variable or r
| ^ B018
|

B018.py:37:5: B018 Found useless expression. Either assign it to a variable or remove it.
|
35 | """my docstring"""
36 | a = 2
37 | "str" # StringLiteral
| ^^^^^ B018
38 | f"{int}" # FString
39 | 1j # Number (complex)
|

B018.py:38:5: B018 Found useless expression. Either assign it to a variable or remove it.
|
36 | a = 2
37 | "str" # StringLiteral
38 | f"{int}" # FString
| ^^^^^^^^ B018
39 | 1j # Number (complex)
40 | 1 # Number (int)
|

B018.py:39:5: B018 Found useless expression. Either assign it to a variable or remove it.
|
37 | "str" # Str (no raise)
38 | f"{int}" # JoinedStr (no raise)
37 | "str" # StringLiteral
38 | f"{int}" # FString
39 | 1j # Number (complex)
| ^^ B018
40 | 1 # Number (int)
Expand All @@ -127,7 +157,7 @@ B018.py:39:5: B018 Found useless expression. Either assign it to a variable or r

B018.py:40:5: B018 Found useless expression. Either assign it to a variable or remove it.
|
38 | f"{int}" # JoinedStr (no raise)
38 | f"{int}" # FString
39 | 1j # Number (complex)
40 | 1 # Number (int)
| ^ B018
Expand Down Expand Up @@ -221,6 +251,15 @@ B018.py:52:5: B018 Found useless expression. Either assign it to a variable or r
54 | "str"
|

B018.py:54:5: B018 Found useless expression. Either assign it to a variable or remove it.
|
52 | 123
53 | a = 2
54 | "str"
| ^^^^^ B018
55 | 3
|

B018.py:55:5: B018 Found useless expression. Either assign it to a variable or remove it.
|
53 | a = 2
Expand Down Expand Up @@ -254,5 +293,3 @@ B018.py:65:5: B018 Found useless expression. Either assign it to a variable or r
65 | "foo" + "bar" # BinOp (raise)
| ^^^^^^^^^^^^^ B018
|


0 comments on commit f7ee407

Please sign in to comment.