Commit graph

19195 commits

Author SHA1 Message Date
bors
e450a27a20 Auto merge of #12372 - GuillaumeGomez:fix-nonminimal_bool-regression, r=flip1995
Fix `nonminimal_bool` lint regression

Fixes #12371.
Fixes #12369.

cc `@RalfJung`

The problem was an invalid condition. Shame on me...

changelog: Fix `nonminimal_bool` lint regression
2024-02-28 15:30:49 +00:00
bors
b4021ee114 Auto merge of #12375 - GuillaumeGomez:improve-code, r=flip1995
Improve `is_lint_level` code

Since https://github.com/rust-lang/rust/pull/121230 was merged, we can now rely on `Level` directly instead of keeping the list of symbols to check in clippy.

changelog: Improve `is_lint_level` code
2024-02-28 15:21:58 +00:00
Guillaume Gomez
41a351665f Improve is_lint_level code 2024-02-28 15:14:22 +01:00
Guillaume Gomez
8473716f6e Add ui regression test for #12371 2024-02-28 12:48:00 +01:00
Guillaume Gomez
ebf20954ee Fix invalid condition in nonminimal_bool lint 2024-02-28 12:47:45 +01:00
bors
4c1d05cfa1 Auto merge of #12362 - Ethiraric:fix-11935, r=llogiq
[`map_entry`]: Check insert expression for map use

The lint makes sure that the map is not used (borrowed) before the call to `insert`. Since the lint creates a mutable borrow on the map with the `Entry`, it wouldn't be possible to replace such code with `Entry`. However, expressions up to the `insert` call are checked, but not expressions for the arguments of the `insert` call itself. This commit fixes that.

Fixes #11935

----

changelog: [`map_entry`]: Fix false positive when borrowing the map in the `insert` call
2024-02-27 18:53:25 +00:00
bors
4155becb2a Auto merge of #12365 - Ethiraric:fix-11968, r=Alexendoo
[`unnecessary_cast`]: Avoid breaking precedence

 If the whole cast expression is a unary expression (`(*x as T)`) or an  addressof expression (`(&x as T)`), then not surrounding the suggestion  into a block risks us changing the precedence of operators if the cast  expression is followed by an operation with higher precedence than the  unary operator (`(*x as T).foo()` would become `*x.foo()`, which  changes what the `*` applies on).
The same is true if the expression encompassing the cast expression is a unary expression or an addressof expression.

The lint supports the latter case, but missed the former one. This PR fixes that.

Fixes #11968

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: [`unnecessary_cast`]: Avoid breaking precedence with unary operators (`(*x as T).foo()` --  before: `*x.foo()` -- now: `{*x}.foo()`)
2024-02-27 17:20:45 +00:00
Ethiraric
c6cb0e99f3 [unnecessary_cast]: Avoid breaking precedence
If the whole cast expression is a unary expression (`(*x as T)`) or an
addressof expression (`(&x as T)`), then not surrounding the suggestion
into a block risks us changing the precedence of operators if the cast
expression is followed by an operation with higher precedence than the
unary operator (`(*x as T).foo()` would become `*x.foo()`, which changes
what the `*` applies on).
The same is true if the expression encompassing the cast expression is a
unary expression or an addressof expression.

The lint supports the latter case, but missed the former one. This PR
fixes that.

Fixes #11968
2024-02-27 16:27:12 +01:00
Ethiraric
03bb7908b9 [map_entry]: Check insert expression for map use
The lint makes sure that the map is not used (borrowed) before the call
to `insert`. Since the lint creates a mutable borrow on the map with the
`Entry`, it wouldn't be possible to replace such code with `Entry`.
However, expressions up to the `insert` call are checked, but not
expressions for the arguments of the `insert` call itself. This commit
fixes that.

Fixes #11935
2024-02-27 15:20:49 +01:00
bors
10136170fe Auto merge of #12363 - oli-obk:bump_ui_test, r=flip1995
lower `bstr` version requirement to `1.6.0`

test changes are due to https://github.com/oli-obk/ui_test/pull/201 (cc `@Jarcho)`

changelog: none
2024-02-27 13:46:14 +00:00
Oli Scherer
592fe89997 lower bstr version requirement to 1.6.0 2024-02-27 13:34:01 +00:00
bors
e33cba523a Auto merge of #12126 - teor2345:patch-1, r=llogiq
Fix sign-handling bugs and false negatives in `cast_sign_loss`

**Note: anyone should feel free to move this PR forward, I might not see notifications from reviewers.**

changelog: [`cast_sign_loss`]: Fix sign-handling bugs and false negatives

This PR fixes some arithmetic bugs and false negatives in PR #11883 (and maybe earlier PRs).
Cc `@J-ZhengLi`

I haven't updated the tests yet. I was hoping for some initial feedback before adding tests to cover the cases listed below.

Here are the issues I've attempted to fix:

#### `abs()` can return a negative value in release builds

Example:
```rust
i32::MIN.abs()
```
https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=022d200f9ef6ee72f629c0c9c1af11b8

Docs: https://doc.rust-lang.org/std/primitive.i32.html#method.abs

Other overflows that produce negative values could cause false negatives (and underflows could produce false positives), but they're harder to detect.

#### Values with uncertain signs can be positive or negative

Any number of values with uncertain signs cause the whole expression to have an uncertain sign, because an uncertain sign can be positive or negative.

Example (from UI tests):
```rust
fn main() {
    foo(a: i32, b: i32, c: i32) -> u32 {
        (a * b * c * c) as u32
        //~^ ERROR: casting `i32` to `u32` may lose the sign of the value
    }

    println!("{}", foo(1, -1, 1));
}
```
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=165d2e2676ee8343b1b9fe60db32aadd

#### Handle `expect()` the same way as `unwrap()`

Since we're ignoring `unwrap()` we might as well do the same with `expect()`.

This doesn't seem to have tests but I'm happy to add some like `Some(existing_test).unwrap() as u32`.

#### A negative base to an odd exponent is guaranteed to be negative

An integer `pow()`'s sign is only uncertain when its operants are uncertain. (Ignoring overflow.)

Example:
```rust
((-2_i32).pow(3) * -2) as u32
```

This offsets some of the false positives created by one or more uncertain signs producing an uncertain sign. (Rather than just an odd number of uncertain signs.)

#### Both sides of a multiply or divide should be peeled recursively

I'm not sure why the lhs was peeled recursively, and the rhs was left intact. But the sign of any sequence of multiplies and divides is determined by the signs of its operands. (Ignoring overflow.)

I'm not sure what to use as an example here, because most expressions I want to use are const-evaluable.

But if `p()` is [a non-const function that returns a positive value](https://doc.rust-lang.org/std/primitive.i32.html#method.isqrt), and if the lint handles unary negation, these should all lint:
```rust
fn peel_all(x: i32) {
    (-p(x) * -p(x) * -p(x)) as u32;
    ((-p(x) * -p(x)) * -p(x)) as u32;
    (-p(x) * (-p(x) * -p(x))) as u32;
}
```

#### The right hand side of a Rem doesn't change the sign

Unlike Mul and Div,
> Given remainder = dividend % divisor, the remainder will have the same sign as the dividend.
https://doc.rust-lang.org/reference/expressions/operator-expr.html#arithmetic-and-logical-binary-operators

I'm not sure what to use as an example here, because most expressions I want to use are const-evaluable.

But if `p()` is [a non-const function that returns a positive value](https://doc.rust-lang.org/std/primitive.i32.html#method.isqrt), and if the lint handles unary negation, only the first six expressions should lint.

The expressions that start with a constant should lint (or not lint) regardless of whether the lint supports `p()` or unary negation, because only the dividend's sign matters.

Example:
```rust
fn rem_lhs(x: i32) {
    (-p(x) % -1) as u32;
    (-p(x) % 1) as u32;
    (-1 % -p(x)) as u32;
    (-1 % p(x)) as u32;
    (-1 % -x) as u32;
    (-1 % x) as u32;
    // These shouldn't lint:
    (p(x) % -1) as u32;
    (p(x) % 1) as u32;
    (1 % -p(x)) as u32;
    (1 % p(x)) as u32;
    (1 % -x) as u32;
    (1 % x) as u32;
}
```

#### There's no need to bail on other expressions

When peeling, any other operators or expressions can be left intact and sent to the constant evaluator.

If these expressions can be evaluated, this offsets some of the false positives created by one or more uncertain signs producing an uncertain sign. If not, they end up marked as having uncertain sign.
2024-02-27 05:38:40 +00:00
bors
fb060815b3 Auto merge of #11136 - y21:enhance_read_line_without_trim, r=dswij
[`read_line_without_trim`]: detect string literal comparison and `.ends_with()` calls

This lint now also realizes that a comparison like `s == "foo"` and calls such as `s.ends_with("foo")` will fail if `s` was initialized by a call to `Stdin::read_line` (because of the trailing newline).

changelog: [`read_line_without_trim`]: detect string literal comparison and `.ends_with()` calls

r? `@giraffate` assigning you because you reviewed #10970 that added this lint, so this is kinda a followup PR ^^
2024-02-27 03:36:12 +00:00
bors
d12b53e481 Auto merge of #12116 - J-ZhengLi:issue12101, r=Alexendoo
fix suggestion error in [`useless_vec`]

fixes: #12101

---

changelog: fix suggestion error in [`useless_vec`]

r+ `@matthiaskrgr` since they opened the issue?
2024-02-26 22:38:24 +00:00
teor
1e3c55eea2
Remove redundant uncertain_counts 2024-02-27 07:34:18 +10:00
y21
fd85db3636 restructure lint code, update description, more cases 2024-02-26 20:24:46 +01:00
y21
cfddd91c91 [read_line_without_trim]: catch string eq checks 2024-02-26 20:19:15 +01:00
bors
1c5094878b Auto merge of #12342 - lucarlig:empty-docs, r=llogiq
Empty docs

Fixes https://github.com/rust-lang/rust-clippy/issues/9931

changelog: [`empty_doc`]: Detects documentation that is empty.
changelog: Doc comment lints now trigger for struct field and enum variant documentation
2024-02-26 18:03:13 +00:00
bors
d71899573a Auto merge of #12355 - Ethiraric:fix-11927, r=Alexendoo
[`box_default`]: Preserve required path segments

When encountering code such as:
```rs
Box::new(outer::Inner::default())
```
clippy would suggest replacing with `Box::<Inner>::default()`, dropping the `outer::` segment. This behavior is incorrect and that commit fixes it.

What it does is it checks the contents of the `Box::new` and, if it is of the form `A::B::default`, does a text replacement, inserting `A::B` in the `Box`'s quickfix generic list.
If the source does not match that pattern (including `Vec::from(..)` or other `T::new()` calls), we then fallback to the original code.

Fixes #11927

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: [`box_default`]: Preserve required path segments
2024-02-26 17:25:43 +00:00
Ethiraric
97dc4b22c6 [box_default]: Preserve required path segments
When encountering code such as:
```
Box::new(outer::Inner::default())
```
clippy would suggest replacing with `Box::<Inner>::default()`, dropping
the `outer::` segment. This behavior is incorrect and that commit fixes
it.

What it does is it checks the contents of the `Box::new` and, if it is
of the form `A::B::default`, does a text replacement, inserting `A::B`
in the `Box`'s quickfix generic list.
If the source does not match that pattern (including `Vec::from(..)`
or other `T::new()` calls), we then fallback to the original code.

Fixes #11927
2024-02-26 17:39:00 +01:00
bors
a11875bd22 Auto merge of #12353 - lucarlig:book, r=flip1995
add cargo.toml lint section way of adding lints in the book

changelog: add cargo.toml lint section way of adding lints in the book

as from [discussion on zulip](https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/.E2.9C.94.20clippy.20config)
2024-02-26 15:53:46 +00:00
Philipp Krones
6d0b70e08f
Clean up Allowing/Denying Lints section 2024-02-26 16:43:05 +01:00
lucarlig
fca77c0976 docs and imports 2024-02-26 19:15:44 +04:00
lucarlig
80bafa5d45 add cargo.toml lint section way of adding lints 2024-02-26 18:59:05 +04:00
bors
b09fb8ab3b Auto merge of #12350 - hamirmahal:style/sync-GitHub-Corner-colors, r=xFrednet
style: sync GitHub Corner colors

fixes #12349.

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: sync site GitHub Corner colors
2024-02-26 08:28:00 +00:00
Hamir Mahal
3ef693c555
refactor: remove CSS variables that are now unused 2024-02-26 00:00:33 -08:00
lucarlig
93deced553 change rs doc to no_run 2024-02-26 07:02:40 +04:00
bors
aa2c94e416 Auto merge of #12308 - y21:more_implied_bounds, r=xFrednet
Look for `implied_bounds_in_impls` in more positions

With this, we lint `impl Trait` implied bounds in more positions:
- Type alias impl trait
- Associated type position impl trait
- Argument position impl trait
  - these are not opaque types, but instead are desugared to `where` clauses, so we need extra logic for finding them (`check_generics`), however the rest of the logic is the same

Before this, we'd only lint RPIT `impl Trait`s.
"Hide whitespaces" and reviewing commits individually might make this easier

changelog: [`implied_bounds_in_impls`]: start linting implied bounds in APIT, ATPIT, TAIT
2024-02-25 22:20:37 +00:00
y21
bbfe1c1ec3 lint implied bounds in APIT 2024-02-25 23:12:28 +01:00
y21
ec29b0d6b8 lint implied bounds in *all* opaque impl Trait types 2024-02-25 23:09:59 +01:00
bors
7353bd0341 Auto merge of #12256 - y21:single_call_fn_rm_visitor, r=llogiq
Merge `single_call_fn` post-crate visitor into lint pass

The `single_call_fn` lint worked by first collecting a list of function definitions in the lint pass, then populating the list of uses for each function in a second visitor after the crate is checked.
Doing another pass through the crate shouldn't be needed, and we should be able to do it in the same lint pass, by looking for path references to functions only and then processing them post-crate.

Other changes:
- `FxHashMap` -> `FxIndexMap` so that we emit warnings in a consistent order, as we see them (making the diff a bit confusing to look at, because warnings were moved around)
- no longer storing a `Vec<Span>` per function: an enum representing "seen once" or "seen more than once" should be enough (only the first element is used later)
- "used here" help is now a note

I also noticed that it lints on trait methods with a default implementation, but not on regular trait methods without a body (because that's what `check_fn` does). I'm not sure if that's useful though, maybe we shouldn't lint trait methods at all? It's not like you can avoid it sometimes (but then again it's a restriction lint). Either way, I left the behavior where it was before so that there are no functional changes made in this PR and it's purely a refactor. I can change it though

changelog: none
2024-02-25 22:02:55 +00:00
Hamir Mahal
30ee7af2ca
style: sync GitHub Corner colors 2024-02-25 12:54:46 -08:00
lucarlig
5152050c5f move lint directly into check_attrs 2024-02-25 23:17:03 +04:00
lucarlig
ee50d5df90 correct wrong doc syntax 2024-02-25 22:52:44 +04:00
lucarlig
d84d9d32f1 lint on variant and fields as well 2024-02-25 22:33:16 +04:00
bors
10c99031ce Auto merge of #12348 - Alexendoo:remove-get-parent-node, r=y21
Remove `clippy_utils::get_parent_node`

Since it's forwarding to a single method it seems reasonable to use that one directly instead

changelog: none
2024-02-25 17:43:59 +00:00
lucarlig
f066be7e1e use span of fragments 2024-02-25 21:31:46 +04:00
lucarlig
d7ad85f521 move the the check into check_atr function 2024-02-25 21:26:43 +04:00
lucarlig
f32e92cdc9 add 1 more test and dont trim other code 2024-02-25 21:18:38 +04:00
Alex Macleod
bee4111a61 Remove clippy_utils::get_parent_node 2024-02-25 16:35:17 +00:00
y21
9a56153c5e [single_call_fn]: merge post-crate visitor into lint pass 2024-02-25 17:13:47 +01:00
bors
c469cb0023 Auto merge of #12336 - not-elm:fix/issue-12243, r=y21
FIX(12243): redundant_guards

Fixed #12243

changelog: Fix[`redundant_guards`]

I have made a correction so that no warning does  appear when y.is_empty() is used within a constant function as follows.

```rust
pub const fn const_fn(x: &str) {
    match x {
        // Shouldn't lint.
        y if y.is_empty() => {},
        _ => {},
    }
}
```
2024-02-25 14:56:07 +00:00
lucarlig
5a50cede29 add single letter test 2024-02-25 18:01:53 +04:00
lucarlig
97e4c57f24 fix lint doc 2024-02-25 17:51:58 +04:00
lucarlig
ee0cbeaa77 ignore empty comment in semicolon_if_nothing_returned 2024-02-25 17:41:41 +04:00
lucarlig
0ea449597e pop before trimming 2024-02-25 17:23:15 +04:00
lucarlig
9ac6125e1d add extra variant because no more than 3 bools 2024-02-25 17:16:07 +04:00
lucarlig
a3fea80a68 bless tests 2024-02-25 17:04:58 +04:00
lucarlig
09c7c5d2c9 fix bug in check_exprs 2024-02-25 16:52:58 +04:00
lucarlig
84219f45a3 working naive with outside check_attrs 2024-02-25 16:11:14 +04:00