-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Don't report function calls as unnecessary operation if used in array index #7453
Don't report function calls as unnecessary operation if used in array index #7453
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @llogiq (or someone else) soon. Please see the contribution instructions for more information. |
Sorry for the late reply, this fell under the radar for some reason. I don't think this is a bug of the lint. IMO the reasoning why this is a bug is flawed:
Hiding away and intentional panic in a indexing operation is bad style. If this actually the intention of this code, you can still allow this lint and add a comment, why this lint is allowed. This at least makes the panic more visible. Clearly this is a semantic change though. But suggesting to remove potentially panicking code is something we allow in Clippy. So what I would do here is to turn the suggestion to |
r? @flip1995 |
91a96d1
to
f922ba1
Compare
Sorry for the back and forth in advance... After thinking about this a bit more, I'm wondering how common this pattern is and if we actually should allow it. On the one hand this fix changes semantics. On the other hand the semantic change removes a possibly unintentional panic from the code and this lint could just be allowed if the panic is actually intentional. Maybe we could just suggest something different for indexing operations, e.g. if arr.len() <= func() { panic!(); } This also optimizes better locally. The question then is if we should keep this in the same lint 🤔 But since I'm not so sure anymore here, @rust-lang/clippy WDYT? |
Going by the lint name, I think it's appropriate to say "this indexing operation is unnecessary," whether or not the user intended to do an assertion. Technically it could be a different lint, but I don't think a new lint is merited to re-classify this anti-pattern. I think we should either add a second suggestion or modify the current suggestion. A second suggestion with its own help message like "if you need to assert the length, write this" is probably better since both possibilities are plausible. |
I agree that adding new lint would be overkill just to cover one edge case. |
For the suggestion I would do assert!(fun() < arr.len()); We should prefer fixed issues over known issues. The fix would be <25 lines of code probably so I don't think code complexity is an issue. |
I would definitely not unconditionally make the suggestion |
Ok, so I think the right way to go forward is:
¹ This suggestion may fail, if |
06b271d
to
1303c76
Compare
48d7627
to
080f38e
Compare
080f38e
to
ede977c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Leaving the PR open for another day, if someone else wants to take another look. Nothing else for you to do @F3real here. Thanks for dealing with my back and forth!
I said 15 days ago and then went on vacation. :) Thanks! @bors r+ |
📌 Commit ede977c has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Attempts to fix: #7412
changelog: Don't report function calls used in indexing as unnecessary operation. [
unnecessary_operation
]