[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>
This commit is contained in:
Giovani Moutinho 2025-09-25 12:19:26 -03:00 committed by GitHub
parent c256c7943c
commit beec2f2dbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 19 additions and 17 deletions

View file

@ -58,7 +58,9 @@ impl Violation for SuppressibleException {
fn fix_title(&self) -> Option<String> {
let SuppressibleException { exception } = self;
Some(format!("Replace with `contextlib.suppress({exception})`"))
Some(format!(
"Replace `try`-`except`-`pass` with `with contextlib.suppress({exception}): ...`"
))
}
}

View file

@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
9 | | pass
| |________^
|
help: Replace with `contextlib.suppress(ValueError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
1 + import contextlib
2 | def foo():
3 | pass
@ -40,7 +40,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`exce
17 |
18 | # SIM105
|
help: Replace with `contextlib.suppress(ValueError, OSError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`
1 + import contextlib
2 | def foo():
3 | pass
@ -71,7 +71,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`exce
23 |
24 | # SIM105
|
help: Replace with `contextlib.suppress(ValueError, OSError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`
1 + import contextlib
2 | def foo():
3 | pass
@ -102,7 +102,7 @@ SIM105 [*] Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`p
29 |
30 | # SIM105
|
help: Replace with `contextlib.suppress(BaseException)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(BaseException): ...`
1 + import contextlib
2 + import builtins
3 | def foo():
@ -134,7 +134,7 @@ SIM105 [*] Use `contextlib.suppress(a.Error, b.Error)` instead of `try`-`except`
35 |
36 | # OK
|
help: Replace with `contextlib.suppress(a.Error, b.Error)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(a.Error, b.Error): ...`
1 + import contextlib
2 | def foo():
3 | pass
@ -164,7 +164,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
88 | | ...
| |___________^
|
help: Replace with `contextlib.suppress(ValueError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
1 + import contextlib
2 | def foo():
3 | pass
@ -195,7 +195,7 @@ SIM105 Use `contextlib.suppress(ValueError, OSError)` instead of `try`-`except`-
104 |
105 | try:
|
help: Replace with `contextlib.suppress(ValueError, OSError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError, OSError): ...`
SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
--> SIM105_0.py:117:5
@ -210,7 +210,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
121 |
122 | try: os.makedirs(model_dir);
|
help: Replace with `contextlib.suppress(OSError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
1 + import contextlib
2 | def foo():
3 | pass
@ -241,7 +241,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
125 |
126 | try: os.makedirs(model_dir);
|
help: Replace with `contextlib.suppress(OSError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
1 + import contextlib
2 | def foo():
3 | pass
@ -271,7 +271,7 @@ SIM105 [*] Use `contextlib.suppress(OSError)` instead of `try`-`except`-`pass`
129 | \
130 | #
|
help: Replace with `contextlib.suppress(OSError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(OSError): ...`
1 + import contextlib
2 | def foo():
3 | pass
@ -299,7 +299,7 @@ SIM105 [*] Use `contextlib.suppress()` instead of `try`-`except`-`pass`
136 | | pass
| |________^
|
help: Replace with `contextlib.suppress()`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(): ...`
1 + import contextlib
2 | def foo():
3 | pass
@ -328,7 +328,7 @@ SIM105 [*] Use `contextlib.suppress(BaseException)` instead of `try`-`except`-`p
143 | | pass
| |________^
|
help: Replace with `contextlib.suppress(BaseException)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(BaseException): ...`
1 + import contextlib
2 | def foo():
3 | pass

View file

@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
8 | | pass
| |________^
|
help: Replace with `contextlib.suppress(ValueError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
1 | """Case: There's a random import, so it should add `contextlib` after it."""
2 | import math
3 + import contextlib

View file

@ -11,7 +11,7 @@ SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass
13 | | pass
| |________^
|
help: Replace with `contextlib.suppress(ValueError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
7 |
8 |
9 | # SIM105

View file

@ -12,4 +12,4 @@ SIM105 Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass`
13 | | pass
| |____________^
|
help: Replace with `contextlib.suppress(ValueError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`

View file

@ -10,7 +10,7 @@ SIM105 [*] Use `contextlib.suppress(ImportError)` instead of `try`-`except`-`pas
4 | | except ImportError: pass
| |___________________________^
|
help: Replace with `contextlib.suppress(ImportError)`
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ImportError): ...`
1 | #!/usr/bin/env python
- try:
2 + import contextlib