rust-clippy/clippy_lints
Matthias Krüger 36214e9838 Rollup merge of #121364 - Urgau:unary_precedence, r=compiler-errors
Implement lint against ambiguous negative literals

This PR implements a lint against ambiguous negative literals with a literal and method calls right after it.

## `ambiguous_negative_literals`

(deny-by-default)

The `ambiguous_negative_literals` lint checks for cases that are confusing between a negative literal and a negation that's not part of the literal.

### Example

```rust,compile_fail
-1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1
```

### Explanation

Method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs.

<details>
<summary>Old proposed lint</summary>

## `ambiguous_unary_precedence`

(deny-by-default)

The `ambiguous_unary_precedence` lint checks for use the negative unary operator with a literal and method calls.

### Example

```rust
-1i32.abs(); // equals -1, while `(-1i32).abs()` equals 1
```

### Explanation

Unary operations take precedence on binary operations and method calls take precedence over unary precedence. Setting the precedence explicitly makes the code clearer and avoid potential bugs.

</details>

-----

Note: This is a strip down version of https://github.com/rust-lang/rust/pull/117161, without the binary op precedence.

Fixes https://github.com/rust-lang/rust/issues/117155
`@rustbot` labels +I-lang-nominated
cc `@scottmcm`
r? compiler
2024-07-25 16:48:17 +02:00
..
src Rollup merge of #121364 - Urgau:unary_precedence, r=compiler-errors 2024-07-25 16:48:17 +02:00
Cargo.toml Merge commit '3e5a02b13b1244545454752c6629b767522a44b1' into clippy-subtree-update 2024-06-13 12:30:48 +02:00
README.md clippy_lints: readme: don't mention crates.io since it is no longer used to publish clippy. 2020-03-10 01:05:54 +01:00

This crate contains Clippy lints. For the main crate, check GitHub.