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

Fix false positive in PLR6301 #7933

Merged
merged 2 commits into from
Oct 14, 2023

Conversation

LaBatata101
Copy link
Contributor

Summary

Don't report a diagnostic if the method contains a super() call.

Closes #6961

Test Plan

cargo test

Don't report a diagnostic if the method contains a `super()` call.
Comment on lines 133 to 151
struct CallVisitor {
is_super: bool,
}

impl<'a> Visitor<'a> for CallVisitor {
fn visit_expr(&mut self, expr: &'a Expr) {
match expr {
Expr::Call(ast::ExprCall { func, .. }) => match func.as_ref() {
Expr::Name(expr_name) => {
self.is_super = expr_name.id == "super";
}
_ => {
visitor::walk_expr(self, expr);
}
},
_ => visitor::walk_expr(self, expr),
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ideally, it would be better to use the function scope to check if the method contains a super() call in the body, but the function scope only gives me access to its parameters. So when using scope.get("super") it would only return None.

Copy link
Member

Choose a reason for hiding this comment

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

It turns out that super is actually a global builtin in Python, so the usages we need are on the global scope. I tweaked the logic to use the technique you suggested above. I guess the downside is that we can't ensure that the super usage was on a super() call, but perhaps that's fine. (We don't store a NodeId on Reference, though we could in the future.)

@github-actions
Copy link
Contributor

github-actions bot commented Oct 13, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

@charliermarsh charliermarsh added the bug Something isn't working label Oct 13, 2023
return;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

@LaBatata101 - What do you think of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LGTM. Didn't know about the binding.references() function, will be very helpful when implementing other rules in the future.

@charliermarsh charliermarsh merged commit e261eb7 into astral-sh:main Oct 14, 2023
16 checks passed
@LaBatata101 LaBatata101 deleted the fix-PLR6301-false-positive branch October 14, 2023 19:46
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.

no-self-use (PLR6301) false positive on methods that call super
2 participants