From a588062b1080101d6985814cc35ed9872afb2520 Mon Sep 17 00:00:00 2001 From: harupy Date: Mon, 23 Oct 2023 20:45:18 +0900 Subject: [PATCH 1/4] Ignore https_proxy, http_proxy, and no_proxy --- .../test/fixtures/flake8_simplify/SIM112.py | 4 ++++ .../src/rules/flake8_simplify/rules/ast_expr.rs | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM112.py b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM112.py index 74b44d63325dc0..937a2555c98e5a 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM112.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_simplify/SIM112.py @@ -38,3 +38,7 @@ if env := os.environ['FOO']: pass + +os.environ['https_proxy'] +os.environ.get['http_proxy'] +os.getenv('no_proxy') diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs index dc3b165b21d56d..9e0d8e81c5e5cc 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs @@ -110,6 +110,10 @@ impl AlwaysFixableViolation for DictGetWithNoneDefault { } } +fn lowercase_allowed(env_var: &str) -> bool { + matches!(env_var, "https_proxy" | "http_proxy" | "no_proxy") +} + /// SIM112 pub(crate) fn use_capital_environment_variables(checker: &mut Checker, expr: &Expr) { // Ex) `os.environ['foo']` @@ -150,6 +154,10 @@ pub(crate) fn use_capital_environment_variables(checker: &mut Checker, expr: &Ex return; } + if lowercase_allowed(env_var) { + return; + } + let capital_env_var = env_var.to_ascii_uppercase(); if &capital_env_var == env_var { return; @@ -194,6 +202,11 @@ fn check_os_environ_subscript(checker: &mut Checker, expr: &Expr) { else { return; }; + + if lowercase_allowed(env_var) { + return; + } + let capital_env_var = env_var.to_ascii_uppercase(); if &capital_env_var == env_var { return; From d00e7ffe1b13de2e3ca18e69a43bbcdaa802ae6e Mon Sep 17 00:00:00 2001 From: harupy Date: Mon, 23 Oct 2023 20:46:58 +0900 Subject: [PATCH 2/4] Rename --- .../ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs index 9e0d8e81c5e5cc..291aaca3fc2b12 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs @@ -110,7 +110,7 @@ impl AlwaysFixableViolation for DictGetWithNoneDefault { } } -fn lowercase_allowed(env_var: &str) -> bool { +fn is_lowercase_allowed(env_var: &str) -> bool { matches!(env_var, "https_proxy" | "http_proxy" | "no_proxy") } @@ -154,7 +154,7 @@ pub(crate) fn use_capital_environment_variables(checker: &mut Checker, expr: &Ex return; } - if lowercase_allowed(env_var) { + if is_lowercase_allowed(env_var) { return; } @@ -203,7 +203,7 @@ fn check_os_environ_subscript(checker: &mut Checker, expr: &Expr) { return; }; - if lowercase_allowed(env_var) { + if is_lowercase_allowed(env_var) { return; } From 7e5305a9937d430cd3ee661362f3dbdc85c16e33 Mon Sep 17 00:00:00 2001 From: harupy Date: Mon, 23 Oct 2023 20:59:06 +0900 Subject: [PATCH 3/4] Docstring --- .../ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs index 291aaca3fc2b12..9670cca72d775d 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs @@ -110,6 +110,11 @@ impl AlwaysFixableViolation for DictGetWithNoneDefault { } } +/// Returns whether the given environment variable is allowed to be lowercase. +/// +/// References: +/// - https://unix.stackexchange.com/a/212972 +/// - https://about.gitlab.com/blog/2021/01/27/we-need-to-talk-no-proxy/#http_proxy-and-https_proxy fn is_lowercase_allowed(env_var: &str) -> bool { matches!(env_var, "https_proxy" | "http_proxy" | "no_proxy") } From c6f6c662b60e3300ff4499983d642ed15fdaac60 Mon Sep 17 00:00:00 2001 From: harupy Date: Mon, 23 Oct 2023 21:12:03 +0900 Subject: [PATCH 4/4] Fix links Signed-off-by: harupy --- .../ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs index 9670cca72d775d..511a0e373b9a3f 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs @@ -113,8 +113,8 @@ impl AlwaysFixableViolation for DictGetWithNoneDefault { /// Returns whether the given environment variable is allowed to be lowercase. /// /// References: -/// - https://unix.stackexchange.com/a/212972 -/// - https://about.gitlab.com/blog/2021/01/27/we-need-to-talk-no-proxy/#http_proxy-and-https_proxy +/// - +/// - fn is_lowercase_allowed(env_var: &str) -> bool { matches!(env_var, "https_proxy" | "http_proxy" | "no_proxy") }