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

Add warning about side-effect free expression-statement #32108

Closed
LukasKalbertodt opened this issue Mar 7, 2016 · 3 comments
Closed

Add warning about side-effect free expression-statement #32108

LukasKalbertodt opened this issue Mar 7, 2016 · 3 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@LukasKalbertodt
Copy link
Member

A programming beginner could write something like

let x = 0;
x + 1;
println!("{}", x);

I would expect to see a warning about this side effect free expression statement in line 2. However, I can imagine that it could be quite hard to determine if such an expression is side effect free. After all, operators for non-standard types could have side effects, too.

Now I don't think some crazy analysis about side-effect-behavior of an expression is useful in this situation. I just thought that some very basic and specialized lints (just for primitive types for example) could be very helpful -- especially for beginners.

@huonw
Copy link
Member

huonw commented Mar 7, 2016

We already have a very simple version of this,

fn main() {
    let x = 1;
    x;
}
<anon>:3:5: 3:7 warning: path statement with no effect, #[warn(path_statements)] on by default
<anon>:3     x;
             ^~

And it doesn't seem that crazy to expand the analysis to include more general effect-less things.

This could be driven by annotations on functions, indicating that they're only interesting for their result, no side-effects, and this could easily end up being similar to #[must_use] rust-lang/rfcs#886.

@huonw huonw added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Mar 7, 2016
@sanxiyn
Copy link
Member

sanxiyn commented Mar 8, 2016

Clippy includes no_effect lint which warns on x + 1; already. Maybe it should move to rustc and replace path_statements lint.

@Mark-Simulacrum Mark-Simulacrum added C-feature-request Category: A feature request, i.e: not implemented / a PR. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Jul 24, 2017
@LukasKalbertodt
Copy link
Member Author

Fixed in #50149

This code:

let x = 0;
x + 1;
println!("{}", x);

Now (1.27) produces:

warning: unused arithmetic operation which must be used
 --> src/main.rs:3:5
  |
3 |     x + 1;
  |     ^^^^^
  |
  = note: #[warn(unused_must_use)] on by default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants