Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-comprehensions] Do not emit unnecessary-map diagnostic when lambda has different arity (C417) #15802

Merged
merged 2 commits into from
Jan 29, 2025

Conversation

InSyncWithFoo
Copy link
Contributor

Summary

Resolves #15796.

The cases discussed in the aforementioned issue are no longer reported.

Test Plan

cargo nextest run and cargo insta test.

Copy link
Contributor

github-actions bot commented Jan 29, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@InSyncWithFoo InSyncWithFoo force-pushed the C417 branch 2 times, most recently from 6e2d14b to 523d349 Compare January 29, 2025 02:44
@AlexWaygood
Copy link
Member

You can do this to fix the Clippy error:

diff --git a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs
index 2b21e1806..8cb0a08f3 100644
--- a/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs
+++ b/crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs
@@ -111,7 +111,7 @@ pub(crate) fn unnecessary_map(
                 return;
             }
 
-            if !lambda_has_expected_arity(parameters.as_ref(), body) {
+            if !lambda_has_expected_arity(parameters.as_deref(), body) {
                 return;
             }
         }
@@ -146,7 +146,7 @@ pub(crate) fn unnecessary_map(
                 return;
             };
 
-            if !lambda_has_expected_arity(parameters.as_ref(), body) {
+            if !lambda_has_expected_arity(parameters.as_deref(), body) {
                 return;
             }
         }
@@ -191,7 +191,7 @@ pub(crate) fn unnecessary_map(
                 return;
             }
 
-            if !lambda_has_expected_arity(parameters.as_ref(), body) {
+            if !lambda_has_expected_arity(parameters.as_deref(), body) {
                 return;
             }
         }
@@ -216,11 +216,10 @@ pub(crate) fn unnecessary_map(
 /// * It has exactly one parameter
 /// * That parameter is not variadic
 /// * That parameter does not have a default value
-fn lambda_has_expected_arity(parameters: Option<&Box<Parameters>>, body: &Expr) -> bool {
+fn lambda_has_expected_arity(parameters: Option<&Parameters>, body: &Expr) -> bool {
     let Some(parameters) = parameters else {
         return false;
     };
-    let parameters = parameters.as_ref();
 
     let [parameter] = &parameters.args[..] else {
         return false;

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@AlexWaygood AlexWaygood changed the title [flake8-comprehensions] Do not emit diagnostic when lambda has different arity (C417) [flake8-comprehensions] Do not emit unnecessary-map diagnostic when lambda has different arity (C417) Jan 29, 2025
@AlexWaygood AlexWaygood enabled auto-merge (squash) January 29, 2025 18:44
@AlexWaygood AlexWaygood added the bug Something isn't working label Jan 29, 2025
@AlexWaygood AlexWaygood merged commit e1c9d10 into astral-sh:main Jan 29, 2025
20 checks passed
@InSyncWithFoo InSyncWithFoo deleted the C417 branch January 29, 2025 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C417 false positive when not exactly 1 lambda parameter
2 participants