Skip to content

Commit beec2f2

Browse files
mgiovaniGiovani Moutinho
andauthored
[flake8-simplify] Improve help message clarity (SIM105) (#20548)
## Summary Improve the SIM105 rule message to prevent user confusion about how to properly use `contextlib.suppress`. The previous message "Replace with `contextlib.suppress(ValueError)`" was ambiguous and led users to incorrectly use `contextlib.suppress(ValueError)` as a statement inside except blocks instead of replacing the entire try-except-pass block with `with contextlib.suppress(ValueError):`. This change makes the message more explicit: - **Before**: `"Use \`contextlib.suppress({exception})\` instead of \`try\`-\`except\`-\`pass\`"` - **After**: `"Replace \`try\`-\`except\`-\`pass\` block with \`with contextlib.suppress({exception})\`"` The fix title is also updated to be more specific: - **Before**: `"Replace with \`contextlib.suppress({exception})\`"` - **After**: `"Replace \`try\`-\`except\`-\`pass\` with \`with contextlib.suppress({exception})\`"` Fixes #20462 ## Test Plan - ✅ All existing SIM105 tests pass with updated snapshots - ✅ Cargo clippy passes without warnings - ✅ Full test suite passes - ✅ The new messages clearly indicate that the entire try-except-pass block should be replaced with a `with` statement, preventing the misuse described in the issue --------- Co-authored-by: Giovani Moutinho <e@mgiovani.dev>
1 parent c256c79 commit beec2f2

6 files changed

Lines changed: 19 additions & 17 deletions

crates/ruff_linter/src/rules/flake8_simplify/rules/suppressible_exception.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ impl Violation for SuppressibleException {
5858

5959
fn fix_title(&self) -> Option<String> {
6060
let SuppressibleException { exception } = self;
61-
Some(format!("Replace with `contextlib.suppress({exception})`"))
61+
Some(format!(
62+
"Replace `try`-`except`-`pass` with `with contextlib.suppress({exception}): ...`"
63+
))
6264
}
6365
}
6466

crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_0.py.snap

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
1111
9 | | pass
1212
| |________^
1313
|
14-
help: Replace with `contextlib.suppress(ValueError)`
14+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
1515
1 + import contextlib
1616
2 | def foo():
1717
3 | pass
@@ -40,7 +40,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`exce
4040
17 |
4141
18 | # SIM105
4242
|
43-
help: Replace with `contextlib.suppress(ValueError, OSError)`
43+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`
4444
1 + import contextlib
4545
2 | def foo():
4646
3 | pass
@@ -71,7 +71,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`exce
7171
23 |
7272
24 | # SIM105
7373
|
74-
help: Replace with `contextlib.suppress(ValueError, OSError)`
74+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`
7575
1 + import contextlib
7676
2 | def foo():
7777
3 | pass
@@ -102,7 +102,7 @@ SIM105 [*] Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`p
102102
29 |
103103
30 | # SIM105
104104
|
105-
help: Replace with `contextlib.suppress(BaseException)`
105+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(BaseException): ...`
106106
1 + import contextlib
107107
2 + import builtins
108108
3 | def foo():
@@ -134,7 +134,7 @@ SIM105 [*] Use `contextlib.suppress(a.Error, b.Error)` instead of `try`-`except`
134134
35 |
135135
36 | # OK
136136
|
137-
help: Replace with `contextlib.suppress(a.Error, b.Error)`
137+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(a.Error, b.Error): ...`
138138
1 + import contextlib
139139
2 | def foo():
140140
3 | pass
@@ -164,7 +164,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
164164
88 | | ...
165165
| |___________^
166166
|
167-
help: Replace with `contextlib.suppress(ValueError)`
167+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
168168
1 + import contextlib
169169
2 | def foo():
170170
3 | pass
@@ -195,7 +195,7 @@ SIM105 Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`except`-
195195
104 |
196196
105 | try:
197197
|
198-
help: Replace with `contextlib.suppress(ValueError, OSError)`
198+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`
199199

200200
SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
201201
--> SIM105_0.py:117:5
@@ -210,7 +210,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
210210
121 |
211211
122 | try: os.makedirs(model_dir);
212212
|
213-
help: Replace with `contextlib.suppress(OSError)`
213+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
214214
1 + import contextlib
215215
2 | def foo():
216216
3 | pass
@@ -241,7 +241,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
241241
125 |
242242
126 | try: os.makedirs(model_dir);
243243
|
244-
help: Replace with `contextlib.suppress(OSError)`
244+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
245245
1 + import contextlib
246246
2 | def foo():
247247
3 | pass
@@ -271,7 +271,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
271271
129 | \
272272
130 | #
273273
|
274-
help: Replace with `contextlib.suppress(OSError)`
274+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
275275
1 + import contextlib
276276
2 | def foo():
277277
3 | pass
@@ -299,7 +299,7 @@ SIM105 [*] Use `contextlib.suppress()` instead of `try`-`except`-`pass`
299299
136 | | pass
300300
| |________^
301301
|
302-
help: Replace with `contextlib.suppress()`
302+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(): ...`
303303
1 + import contextlib
304304
2 | def foo():
305305
3 | pass
@@ -328,7 +328,7 @@ SIM105 [*] Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`p
328328
143 | | pass
329329
| |________^
330330
|
331-
help: Replace with `contextlib.suppress(BaseException)`
331+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(BaseException): ...`
332332
1 + import contextlib
333333
2 | def foo():
334334
3 | pass

crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_1.py.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
1111
8 | | pass
1212
| |________^
1313
|
14-
help: Replace with `contextlib.suppress(ValueError)`
14+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
1515
1 | """Case: There's a random import, so it should add `contextlib` after it."""
1616
2 | import math
1717
3 + import contextlib

crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_2.py.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
1111
13 | | pass
1212
| |________^
1313
|
14-
help: Replace with `contextlib.suppress(ValueError)`
14+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
1515
7 |
1616
8 |
1717
9 | # SIM105

crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_3.py.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ SIM105 Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass`
1212
13 | | pass
1313
| |____________^
1414
|
15-
help: Replace with `contextlib.suppress(ValueError)`
15+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`

crates/ruff_linter/src/rules/flake8_simplify/snapshots/ruff_linter__rules__flake8_simplify__tests__SIM105_SIM105_4.py.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SIM105 [*] Use `contextlib.suppress(ImportError)` instead of `try`-`except`-`pas
1010
4 | | except ImportError: pass
1111
| |___________________________^
1212
|
13-
help: Replace with `contextlib.suppress(ImportError)`
13+
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ImportError): ...`
1414
1 | #!/usr/bin/env python
1515
- try:
1616
2 + import contextlib

0 commit comments

Comments
 (0)