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

immutable while condition lint should not trigger on constants #2514

Closed
oli-obk opened this issue Mar 7, 2018 · 7 comments
Closed

immutable while condition lint should not trigger on constants #2514

oli-obk opened this issue Mar 7, 2018 · 7 comments
Labels
C-bug Category: Clippy is not doing the correct thing S-needs-discussion Status: Needs further discussion before merging or work can be started

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Mar 7, 2018

cc @kimsnj

currently while false {} triggers the lint, while SOME_CONST {} would also trigger it, neither of them should. The condition should contain some local in order for the lint to trigger.

@oli-obk oli-obk added the C-bug Category: Clippy is not doing the correct thing label Mar 7, 2018
@kimsnj
Copy link
Contributor

kimsnj commented Mar 7, 2018

Ooops! I initially had a "has_var" check that I must have removed while moving code to "visitor".

But what would be the usecase for while SOME_CONST {} ?
Isn't it still worth it to trigger the lint in this case?

@oli-obk
Copy link
Contributor Author

oli-obk commented Mar 7, 2018

@kimsnj I'm not 100% sure on what we should be doing here. Because you are catching

let x = false;
while x {}

Which is absolutely correct, the condition is useless because it cannot change, but maybe the user meant to iterate if some condition is met, but not otherwise, and the loop will terminate by other means.

"Cleaner" would be if x { loop {} }.

I don't really know how to move forward from here.

@oli-obk oli-obk added the S-needs-discussion Status: Needs further discussion before merging or work can be started label Mar 7, 2018
@kimsnj
Copy link
Contributor

kimsnj commented Mar 7, 2018

@oli-obk We could push the reasoning a bit further…

let x = 5;
while x > 1 {}

Should the above snippet be caught or not?
What should be a tolerably complex condition that could be skipped.

Maybe we could split the lint in 2 diffrent ones:

  • Constant condition:
    That covers above cases (both literals and Constants).
    Such a lint could be considered part of "clippy_pedantic".
    I realized that there seems to be a const evaluator (in Clippy or rustc?) used in some lints.
    If we manage to evaluate this condition, we could adapt the warning to say wether it's an infinite loop or dead code.

  • Unused mutable variable:
    The second part of the lint detects if a mutable var is not used immutably in the body.
    This one could be kept as part of standard lints.

Not directly related to this (as it might actually impact both), what I find a bit lacking in this lint is that cases such as:

let mut vec: Vec::new();
while vec.len() > 0 {
	println!("Hello world");
}

are note covered.
I guess it's a pretty common condition.
I don't know to which extent we could whitelist some methods implemented within some modules such as alloc::vec, std::collections, core::option and core::result

@oli-obk
Copy link
Contributor Author

oli-obk commented Mar 7, 2018

That specific one results in

warning: length comparison to zero
 --> src/main.rs:4:11
  |
4 |     while vec.len() > 0 {
  |           ^^^^^^^^^^^^^ help: using `is_empty` is more concise: `!vec.is_empty()`

so I'm not worried.

but I see your point. If we allowed immutable method/function calls and added a check for interior mutability then those cases should be covered, too.

We don't need to be perfect, just without false positives ;)

@oli-obk
Copy link
Contributor Author

oli-obk commented Mar 7, 2018

let x = 5;
while x > 1 {}

should be linted, because x is never mentioned in the body.

The current lint message could be adjusted to might lead to infinite loops or never running loops

@kimsnj
Copy link
Contributor

kimsnj commented Mar 7, 2018

Actually checking that the method only takes its argument immutably wouldn't be sufficient (e.g. generate a random number / reads from file …). Thus having some white-listed modules for which we do not directly bail.

@oli-obk
Copy link
Contributor Author

oli-obk commented Mar 7, 2018

oh... right. we'd need purity... well... let's just stay with the obvious numeric cases. And yea, just throw the const evaluator on it. if it returns anything, don't lint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing S-needs-discussion Status: Needs further discussion before merging or work can be started
Projects
None yet
Development

No branches or pull requests

2 participants