Skip to content

Uplift clippy::double_neg into rustc #82987

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
rbtcollins opened this issue Mar 10, 2021 · 3 comments · Fixed by #126604
Closed

Uplift clippy::double_neg into rustc #82987

rbtcollins opened this issue Mar 10, 2021 · 3 comments · 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`
@bors bors closed this as completed in 3c58b2f Jan 27, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 27, 2025
Rollup merge of rust-lang#126604 - kadiwa4:uplift_double_negation, r=nnethercote

Uplift `clippy::double_neg` lint as `double_negations`

Warns about cases like this:
```rust
fn main() {
    let x = 1;
    let _b = --x; //~ WARN use of a double negation
}
```

The intent is to keep people from thinking that `--x` is a prefix decrement operator. `++x`, `x++` and `x--` are invalid expressions and already have a helpful diagnostic.

I didn't add a machine-applicable suggestion to the lint because it's not entirely clear what the programmer was trying to achieve with the `--x` operation. The code that triggers the lint should always be reviewed manually.

Closes rust-lang#82987
bors pushed a commit to rust-lang-ci/rust that referenced this issue Jan 28, 2025
…nnethercote

Uplift `clippy::double_neg` lint as `double_negations`

Warns about cases like this:
```rust
fn main() {
    let x = 1;
    let _b = --x; //~ WARN use of a double negation
}
```

The intent is to keep people from thinking that `--x` is a prefix decrement operator. `++x`, `x++` and `x--` are invalid expressions and already have a helpful diagnostic.

I didn't add a machine-applicable suggestion to the lint because it's not entirely clear what the programmer was trying to achieve with the `--x` operation. The code that triggers the lint should always be reviewed manually.

Closes rust-lang#82987
# for free to join this conversation on GitHub. Already have an account? # 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