Remove autofix behavior for uncapitalized-environment-variables (`SIM112`) (#3988)

This commit is contained in:
Charlie Marsh 2023-04-16 19:19:05 -04:00 committed by GitHub
parent bd78c6ade2
commit 1cdd5e3424
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 85 deletions

View File

@ -1,6 +1,6 @@
use rustpython_parser::ast::{Constant, Expr, ExprKind};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::{create_expr, unparse_expr};
use ruff_python_ast::types::Range;
@ -33,35 +33,30 @@ pub struct DictGetWithNoneDefault {
pub original: String,
}
impl AlwaysAutofixableViolation for DictGetWithNoneDefault {
impl Violation for DictGetWithNoneDefault {
#[derive_message_formats]
fn message(&self) -> String {
let DictGetWithNoneDefault { expected, original } = self;
format!("Use `{expected}` instead of `{original}`")
}
fn autofix_title(&self) -> String {
let DictGetWithNoneDefault { expected, original } = self;
format!("Replace `{original}` with `{expected}`")
}
}
/// SIM112
pub fn use_capital_environment_variables(checker: &mut Checker, expr: &Expr) {
// check `os.environ['foo']`
// Ex) `os.environ['foo']`
if let ExprKind::Subscript { .. } = &expr.node {
check_os_environ_subscript(checker, expr);
return;
}
// check `os.environ.get('foo')` and `os.getenv('foo')`.
// Ex) `os.environ.get('foo')`, `os.getenv('foo')`
let ExprKind::Call { func, args, .. } = &expr.node else {
return;
};
let Some(arg) = args.get(0) else {
return;
};
let ExprKind::Constant { value: Constant::Str(env_var), kind } = &arg.node else {
let ExprKind::Constant { value: Constant::Str(env_var), .. } = &arg.node else {
return;
};
if !checker
@ -80,25 +75,13 @@ pub fn use_capital_environment_variables(checker: &mut Checker, expr: &Expr) {
return;
}
let mut diagnostic = Diagnostic::new(
checker.diagnostics.push(Diagnostic::new(
UncapitalizedEnvironmentVariables {
expected: capital_env_var.clone(),
expected: capital_env_var,
original: env_var.clone(),
},
Range::from(arg),
);
if checker.patch(diagnostic.kind.rule()) {
let new_env_var = create_expr(ExprKind::Constant {
value: capital_env_var.into(),
kind: kind.clone(),
});
diagnostic.set_fix(Edit::replacement(
unparse_expr(&new_env_var, checker.stylist),
arg.location,
arg.end_location.unwrap(),
));
}
checker.diagnostics.push(diagnostic);
}
fn check_os_environ_subscript(checker: &mut Checker, expr: &Expr) {

View File

@ -32,16 +32,6 @@ SIM112.py:6:16: SIM112 [*] Use capitalized environment variable `FOO` instead of
|
= help: Replace `foo` with `FOO`
Suggested fix
3 3 | # Bad
4 4 | os.environ['foo']
5 5 |
6 |-os.environ.get('foo')
6 |+os.environ.get('FOO')
7 7 |
8 8 | os.environ.get('foo', 'bar')
9 9 |
SIM112.py:8:16: SIM112 [*] Use capitalized environment variable `FOO` instead of `foo`
|
8 | os.environ.get('foo')
@ -53,16 +43,6 @@ SIM112.py:8:16: SIM112 [*] Use capitalized environment variable `FOO` instead of
|
= help: Replace `foo` with `FOO`
Suggested fix
5 5 |
6 6 | os.environ.get('foo')
7 7 |
8 |-os.environ.get('foo', 'bar')
8 |+os.environ.get('FOO', 'bar')
9 9 |
10 10 | os.getenv('foo')
11 11 |
SIM112.py:10:11: SIM112 [*] Use capitalized environment variable `FOO` instead of `foo`
|
10 | os.environ.get('foo', 'bar')
@ -74,16 +54,6 @@ SIM112.py:10:11: SIM112 [*] Use capitalized environment variable `FOO` instead o
|
= help: Replace `foo` with `FOO`
Suggested fix
7 7 |
8 8 | os.environ.get('foo', 'bar')
9 9 |
10 |-os.getenv('foo')
10 |+os.getenv('FOO')
11 11 |
12 12 | env = os.environ.get('foo')
13 13 |
SIM112.py:12:22: SIM112 [*] Use capitalized environment variable `FOO` instead of `foo`
|
12 | os.getenv('foo')
@ -95,16 +65,6 @@ SIM112.py:12:22: SIM112 [*] Use capitalized environment variable `FOO` instead o
|
= help: Replace `foo` with `FOO`
Suggested fix
9 9 |
10 10 | os.getenv('foo')
11 11 |
12 |-env = os.environ.get('foo')
12 |+env = os.environ.get('FOO')
13 13 |
14 14 | env = os.environ['foo']
15 15 |
SIM112.py:14:18: SIM112 [*] Use capitalized environment variable `FOO` instead of `foo`
|
14 | env = os.environ.get('foo')
@ -136,16 +96,6 @@ SIM112.py:16:26: SIM112 [*] Use capitalized environment variable `FOO` instead o
|
= help: Replace `foo` with `FOO`
Suggested fix
13 13 |
14 14 | env = os.environ['foo']
15 15 |
16 |-if env := os.environ.get('foo'):
16 |+if env := os.environ.get('FOO'):
17 17 | pass
18 18 |
19 19 | if env := os.environ['foo']:
SIM112.py:19:22: SIM112 [*] Use capitalized environment variable `FOO` instead of `foo`
|
19 | pass

View File

@ -1,7 +1,7 @@
---
source: crates/ruff/src/rules/flake8_simplify/mod.rs
---
SIM910.py:2:1: SIM910 [*] Use `{}.get(key)` instead of `{}.get(key, None)`
SIM910.py:2:1: SIM910 Use `{}.get(key)` instead of `{}.get(key, None)`
|
2 | # SIM910
3 | {}.get(key, None)
@ -9,7 +9,6 @@ SIM910.py:2:1: SIM910 [*] Use `{}.get(key)` instead of `{}.get(key, None)`
4 |
5 | # SIM910
|
= help: Replace `{}.get(key, None)` with `{}.get(key)`
Suggested fix
1 1 | # SIM910
@ -19,7 +18,7 @@ SIM910.py:2:1: SIM910 [*] Use `{}.get(key)` instead of `{}.get(key, None)`
4 4 | # SIM910
5 5 | {}.get("key", None)
SIM910.py:5:1: SIM910 [*] Use `{}.get("key")` instead of `{}.get("key", None)`
SIM910.py:5:1: SIM910 Use `{}.get("key")` instead of `{}.get("key", None)`
|
5 | # SIM910
6 | {}.get("key", None)
@ -27,7 +26,6 @@ SIM910.py:5:1: SIM910 [*] Use `{}.get("key")` instead of `{}.get("key", None)`
7 |
8 | # OK
|
= help: Replace `{}.get("key", None)` with `{}.get("key")`
Suggested fix
2 2 | {}.get(key, None)
@ -39,14 +37,13 @@ SIM910.py:5:1: SIM910 [*] Use `{}.get("key")` instead of `{}.get("key", None)`
7 7 | # OK
8 8 | {}.get(key)
SIM910.py:20:9: SIM910 [*] Use `{}.get(key)` instead of `{}.get(key, None)`
SIM910.py:20:9: SIM910 Use `{}.get(key)` instead of `{}.get(key, None)`
|
20 | # SIM910
21 | if a := {}.get(key, None):
| ^^^^^^^^^^^^^^^^^ SIM910
22 | pass
|
= help: Replace `{}.get(key, None)` with `{}.get(key)`
Suggested fix
17 17 | {}.get("key", False)
@ -58,7 +55,7 @@ SIM910.py:20:9: SIM910 [*] Use `{}.get(key)` instead of `{}.get(key, None)`
22 22 |
23 23 | # SIM910
SIM910.py:24:5: SIM910 [*] Use `{}.get(key)` instead of `{}.get(key, None)`
SIM910.py:24:5: SIM910 Use `{}.get(key)` instead of `{}.get(key, None)`
|
24 | # SIM910
25 | a = {}.get(key, None)
@ -66,7 +63,6 @@ SIM910.py:24:5: SIM910 [*] Use `{}.get(key)` instead of `{}.get(key, None)`
26 |
27 | # SIM910
|
= help: Replace `{}.get(key, None)` with `{}.get(key)`
Suggested fix
21 21 | pass
@ -78,13 +74,12 @@ SIM910.py:24:5: SIM910 [*] Use `{}.get(key)` instead of `{}.get(key, None)`
26 26 | # SIM910
27 27 | ({}).get(key, None)
SIM910.py:27:1: SIM910 [*] Use `({}).get(key)` instead of `({}).get(key, None)`
SIM910.py:27:1: SIM910 Use `({}).get(key)` instead of `({}).get(key, None)`
|
27 | # SIM910
28 | ({}).get(key, None)
| ^^^^^^^^^^^^^^^^^^^ SIM910
|
= help: Replace `({}).get(key, None)` with `({}).get(key)`
Suggested fix
24 24 | a = {}.get(key, None)