Skip to content

Commit c8aedc0

Browse files
committed
B005: Reflect method name in diagnostic, adapt range
The diagnostic message now reports the method name, and range was adapted to be more precise and only cover the call and argument.
1 parent 5e698d5 commit c8aedc0

4 files changed

Lines changed: 59 additions & 30 deletions

File tree

‎crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B005.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
s.strip("ああ") # warning
2323
s.strip("\ufeff") # no warning
2424
s.strip("\u0074\u0065\u0073\u0074") # warning
25+
s.strip("--").lstrip("--") # two warnings
2526

2627
from somewhere import other_type, strip
2728

‎crates/ruff_linter/src/rules/flake8_bugbear/rules/strip_with_multi_characters.rs‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use itertools::Itertools;
22
use ruff_python_ast::{self as ast, Expr};
33

44
use ruff_macros::{ViolationMetadata, derive_message_formats};
5-
use ruff_text_size::Ranged;
5+
use ruff_text_size::{Ranged, TextRange};
66

77
use crate::Violation;
88
use crate::checkers::ast::Checker;
9+
use crate::rules::pylint::rules::StripKind;
910

1011
/// ## What it does
1112
/// Checks for uses of multi-character strings in `.strip()`, `.lstrip()`, and
@@ -43,15 +44,18 @@ use crate::checkers::ast::Checker;
4344
/// ```
4445
///
4546
/// ## References
46-
/// - [Python documentation: `str.strip`](https://docs.python.org/3/library/stdtypes.html#str.strip)
47+
/// - [Python documentation: `str.strip()`](https://docs.python.org/3/library/stdtypes.html#str.strip)
4748
#[derive(ViolationMetadata)]
4849
#[violation_metadata(stable_since = "v0.0.106")]
49-
pub(crate) struct StripWithMultiCharacters;
50+
pub(crate) struct StripWithMultiCharacters {
51+
strip: StripKind,
52+
}
5053

5154
impl Violation for StripWithMultiCharacters {
5255
#[derive_message_formats]
5356
fn message(&self) -> String {
54-
"Using `.strip()` with multi-character strings is misleading".to_string()
57+
let Self { strip } = self;
58+
format!("Using `.{strip}()` with multi-character strings is misleading")
5559
}
5660
}
5761

@@ -65,15 +69,18 @@ pub(crate) fn strip_with_multi_characters(
6569
let Expr::Attribute(ast::ExprAttribute { attr, .. }) = func else {
6670
return;
6771
};
68-
if !matches!(attr.as_str(), "strip" | "lstrip" | "rstrip") {
72+
let Some(strip) = StripKind::from_str(attr.as_str()) else {
6973
return;
70-
}
74+
};
7175

7276
let [Expr::StringLiteral(ast::ExprStringLiteral { value, .. })] = args else {
7377
return;
7478
};
7579

7680
if value.chars().count() > 1 && !value.chars().all_unique() {
77-
checker.report_diagnostic(StripWithMultiCharacters, expr.range());
81+
checker.report_diagnostic(
82+
StripWithMultiCharacters { strip },
83+
TextRange::new(attr.start(), expr.end()),
84+
);
7885
}
7986
}

‎crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B005_B005.py.snap‎

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,110 @@
22
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
33
---
44
B005 Using `.strip()` with multi-character strings is misleading
5-
--> B005.py:4:1
5+
--> B005.py:4:3
66
|
77
2 | s.strip(s) # no warning
88
3 | s.strip("we") # no warning
99
4 | s.strip(".facebook.com") # warning
10-
| ^^^^^^^^^^^^^^^^^^^^^^^^
10+
| ^^^^^^^^^^^^^^^^^^^^^^
1111
5 | s.strip("e") # no warning
1212
6 | s.strip("\n\t ") # no warning
1313
|
1414

1515
B005 Using `.strip()` with multi-character strings is misleading
16-
--> B005.py:7:1
16+
--> B005.py:7:3
1717
|
1818
5 | s.strip("e") # no warning
1919
6 | s.strip("\n\t ") # no warning
2020
7 | s.strip(r"\n\t ") # warning
21-
| ^^^^^^^^^^^^^^^^^
21+
| ^^^^^^^^^^^^^^^
2222
8 | s.lstrip(s) # no warning
2323
9 | s.lstrip("we") # no warning
2424
|
2525

26-
B005 Using `.strip()` with multi-character strings is misleading
27-
--> B005.py:10:1
26+
B005 Using `.lstrip()` with multi-character strings is misleading
27+
--> B005.py:10:3
2828
|
2929
8 | s.lstrip(s) # no warning
3030
9 | s.lstrip("we") # no warning
3131
10 | s.lstrip(".facebook.com") # warning
32-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
32+
| ^^^^^^^^^^^^^^^^^^^^^^^
3333
11 | s.lstrip("e") # no warning
3434
12 | s.lstrip("\n\t ") # no warning
3535
|
3636

37-
B005 Using `.strip()` with multi-character strings is misleading
38-
--> B005.py:13:1
37+
B005 Using `.lstrip()` with multi-character strings is misleading
38+
--> B005.py:13:3
3939
|
4040
11 | s.lstrip("e") # no warning
4141
12 | s.lstrip("\n\t ") # no warning
4242
13 | s.lstrip(r"\n\t ") # warning
43-
| ^^^^^^^^^^^^^^^^^^
43+
| ^^^^^^^^^^^^^^^^
4444
14 | s.rstrip(s) # no warning
4545
15 | s.rstrip("we") # warning
4646
|
4747

48-
B005 Using `.strip()` with multi-character strings is misleading
49-
--> B005.py:16:1
48+
B005 Using `.rstrip()` with multi-character strings is misleading
49+
--> B005.py:16:3
5050
|
5151
14 | s.rstrip(s) # no warning
5252
15 | s.rstrip("we") # warning
5353
16 | s.rstrip(".facebook.com") # warning
54-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
54+
| ^^^^^^^^^^^^^^^^^^^^^^^
5555
17 | s.rstrip("e") # no warning
5656
18 | s.rstrip("\n\t ") # no warning
5757
|
5858

59-
B005 Using `.strip()` with multi-character strings is misleading
60-
--> B005.py:19:1
59+
B005 Using `.rstrip()` with multi-character strings is misleading
60+
--> B005.py:19:3
6161
|
6262
17 | s.rstrip("e") # no warning
6363
18 | s.rstrip("\n\t ") # no warning
6464
19 | s.rstrip(r"\n\t ") # warning
65-
| ^^^^^^^^^^^^^^^^^^
65+
| ^^^^^^^^^^^^^^^^
6666
20 | s.strip("a") # no warning
6767
21 | s.strip("あ") # no warning
6868
|
6969

7070
B005 Using `.strip()` with multi-character strings is misleading
71-
--> B005.py:22:1
71+
--> B005.py:22:3
7272
|
7373
20 | s.strip("a") # no warning
7474
21 | s.strip("あ") # no warning
7575
22 | s.strip("ああ") # warning
76-
| ^^^^^^^^^^^^^^^
76+
| ^^^^^^^^^^^^^
7777
23 | s.strip("\ufeff") # no warning
7878
24 | s.strip("\u0074\u0065\u0073\u0074") # warning
7979
|
8080

8181
B005 Using `.strip()` with multi-character strings is misleading
82-
--> B005.py:24:1
82+
--> B005.py:24:3
8383
|
8484
22 | s.strip("ああ") # warning
8585
23 | s.strip("\ufeff") # no warning
8686
24 | s.strip("\u0074\u0065\u0073\u0074") # warning
87-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88-
25 |
89-
26 | from somewhere import other_type, strip
87+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88+
25 | s.strip("--").lstrip("--") # two warnings
89+
|
90+
91+
B005 Using `.strip()` with multi-character strings is misleading
92+
--> B005.py:25:3
93+
|
94+
23 | s.strip("\ufeff") # no warning
95+
24 | s.strip("\u0074\u0065\u0073\u0074") # warning
96+
25 | s.strip("--").lstrip("--") # two warnings
97+
| ^^^^^^^^^^^
98+
26 |
99+
27 | from somewhere import other_type, strip
100+
|
101+
102+
B005 Using `.lstrip()` with multi-character strings is misleading
103+
--> B005.py:25:15
104+
|
105+
23 | s.strip("\ufeff") # no warning
106+
24 | s.strip("\u0074\u0065\u0073\u0074") # warning
107+
25 | s.strip("--").lstrip("--") # two warnings
108+
| ^^^^^^^^^^^^
109+
26 |
110+
27 | from somewhere import other_type, strip
90111
|

‎crates/ruff_linter/src/rules/pylint/rules/bad_str_strip_call.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use ruff_python_ast::PythonVersion;
4646
/// - `target-version`
4747
///
4848
/// ## References
49-
/// - [Python documentation: `str.strip`](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.strip)
49+
/// - [Python documentation: `str.strip()`](https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.strip)
5050
#[derive(ViolationMetadata)]
5151
#[violation_metadata(stable_since = "v0.0.242")]
5252
pub(crate) struct BadStrStripCall {

0 commit comments

Comments
 (0)