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

Uplift clippy::double_neg into rustc #82987

Open
rbtcollins opened this issue Mar 10, 2021 · 3 comments · May be fixed by #126604
Open

Uplift clippy::double_neg into rustc #82987

rbtcollins opened this issue Mar 10, 2021 · 3 comments · May be fixed by #126604
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rbtcollins
Copy link
Contributor

See this https://dtolnay.github.io/rust-quiz/17

I agree with our position on the inclusion of --/++ in Rust, however currently we just silently treat --i as -(-i), which means that users who are coming from C++ and C get absolutely no warning that their code is going to do something entirely different than they expect - and there's arguably no use case for an unbracketed - -i : its the same as a literal i.

I expected to see this happen:

A warning, or an error: "attempt to use unsupported post-increment operator. Use +=1 instead". Or something like that.

Instead, this happened: explanation

Meta

I'm not sure what rustc version the rust-quiz website is running.

rustc --version --verbose:

<version>
Backtrace

<backtrace>

@rbtcollins rbtcollins added the C-bug Category: This is a bug. label Mar 10, 2021
@Aaron1011
Copy link
Member

A concrete example:

fn main() {
    let a = 1;
    let _b = --a;
}

compiles without any warnings

@Aaron1011 Aaron1011 added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Mar 10, 2021
@leonardo-m
Copy link

leonardo-m commented Mar 10, 2021

Clippy spots it:

warning: `--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op
 --> src/main.rs:3:14
  |
3 |     let _b = --a;
  |              ^^^
  |
  = note: `#[warn(clippy::double_neg)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#double_neg

So you're asking to move this lint from Clippy to rustc.
I think we could turn it into a rustc error.

@jyn514 jyn514 changed the title {pre,post}-{increment,decrement} are silently interpreted as successive unary operations Uplift clippy::double_neg into rustc Mar 10, 2021
@jyn514 jyn514 added C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed C-bug Category: This is a bug. labels Mar 10, 2021
@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 26, 2021
@Flying-Toast
Copy link
Contributor

I think I can tackle this one - should I start a PR?

bors added a commit to rust-lang-ci/rust that referenced this issue Apr 3, 2022
Suggest `i += 1` when we see `i++` or `++i`

Closes rust-lang#83502 (for `i++` and `++i`; `--i` should be covered by rust-lang#82987, and `i--`
is tricky to handle).

This is a continuation of rust-lang#83536.

r? `@estebank`
@kadiwa4 kadiwa4 linked a pull request Jun 17, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants