Commit graph

6885 commits

Author SHA1 Message Date
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
y21
fd85db3636 restructure lint code, update description, more cases 2024-02-26 20:24:46 +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
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
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
lucarlig
d84d9d32f1 lint on variant and fields as well 2024-02-25 22:33:16 +04:00
lucarlig
f32e92cdc9 add 1 more test and dont trim other code 2024-02-25 21:18:38 +04: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
ee0cbeaa77 ignore empty comment in semicolon_if_nothing_returned 2024-02-25 17:41:41 +04:00
lucarlig
a3fea80a68 bless tests 2024-02-25 17:04:58 +04:00
lucarlig
84219f45a3 working naive with outside check_attrs 2024-02-25 16:11:14 +04:00
not-elm
5a63cd82cb FIX(12243): redundant_guard
A warning is now suppressed when "<str_va> if <str_var>.is_empty" is used in a constant function.

FIX: instead of clippy_util::in_const

FIX: Merged `redundant_guards_const_fn.rs` into `redundant_guards.rs`.
2024-02-25 15:38:18 +09:00
lucarlig
3093b291f6 WIP: empty doc span is still broken 2024-02-25 09:55:58 +04:00
Guillaume Gomez
a1971414ec Update ui tests 2024-02-24 15:02:10 +01:00
Guillaume Gomez
40cff2d99f Add ui test for unnecessary_get_then_check 2024-02-24 15:02:10 +01:00
bors
a2c1d565e5 Auto merge of #12259 - GuillaumeGomez:multiple-bound-locations, r=llogiq
Add new `multiple_bound_locations` lint

Fixes #7181.

r? `@llogiq`

changelog: Add new `multiple_bound_locations` lint
2024-02-24 13:43:34 +00:00
bors
64054693eb Auto merge of #12322 - sanxiyn:expression-with-attribute, r=llogiq
Be careful with expressions with attributes

Fix #9949.

changelog: [`unused_unit`]: skip expressions with attributes
2024-02-24 10:52:55 +00:00
Jason Newcomb
5ab42d8e6a Take lifetime extension into account in ref_as_ptr 2024-02-23 21:33:53 -05:00
Guillaume Gomez
762448bc55 Update ui tests 2024-02-23 17:38:39 +01:00
J-ZhengLi
929f746b5c fix the actual bug 2024-02-24 00:35:48 +08:00
Guillaume Gomez
6955a8ac4e Add ui test for multiple_bound_locations lint 2024-02-23 16:40:21 +01:00
MarcusGrass
f1974593c9
Remove double unused_imports check 2024-02-22 23:12:38 +01:00
MarcusGrass
97a3ac5b86
Allow unused_imports, and unused_import_braces on use 2024-02-22 21:53:04 +01:00
Philipp Krones
8a58b7613d
Update i686 asm test stderr 2024-02-22 16:27:24 +01:00
Philipp Krones
dc0bb69e66
Merge remote-tracking branch 'upstream/master' into rustup 2024-02-22 15:59:29 +01:00
bors
d554bcad79 Auto merge of #12303 - GuillaumeGomez:unneedeed_clippy_cfg_attr, r=flip1995
Add `unnecessary_clippy_cfg` lint

Follow-up of https://github.com/rust-lang/rust-clippy/pull/12292.

r? `@flip1995`

changelog: Add `unnecessary_clippy_cfg` lint
2024-02-22 11:00:52 +00:00
Guillaume Gomez
cf6a14cea1 Add ui test for unneeded_clippy_cfg_attr 2024-02-22 11:55:31 +01:00
Christopher B. Speir
b72996e322 Add check for 'in_external_macro' and 'is_from_proc_macro' inside [infinite_loop] lint. 2024-02-21 16:34:07 -06:00
bors
250fd09405 Auto merge of #12324 - GuillaumeGomez:useless_allocation2, r=y21
Extend `unnecessary_to_owned` to handle `Borrow` trait in map types

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

Alternative to #12315.

r? `@y21`

changelog: Extend `unnecessary_to_owned` to handle `Borrow` trait in map types
2024-02-21 21:38:41 +00:00
Guillaume Gomez
d28146133c Add more ui tests for unnecessary_to_owned 2024-02-21 19:32:08 +01:00
taiga.watanabe
aa8a82ec26 FIX: issue-12279
----
UPDATE: add async block into test.

FIX: no_effect

Fixed asynchronous function parameter names with underscores so that warnings are not displayed when underscores are added to parameter names

ADD: test case
2024-02-21 23:26:29 +09:00
Seo Sanghyeon
cd45d5a81c Be careful with expressions with attributes 2024-02-20 22:18:49 +09:00
bors
ba2139afd6 Auto merge of #121087 - oli-obk:eager_const_failures, r=lcnr
Always evaluate free constants and statics, even if previous errors occurred

work towards https://github.com/rust-lang/rust/issues/79738

We will need to evaluate static items before the `definitions.freeze()` below, as we will start creating new `DefId`s (for nested allocations) within the `eval_static_initializer` query.

But even without that motivation, this is a good change. Hard errors should always be reported and not silenced if other errors happened earlier.
2024-02-20 09:02:34 +00:00
Oli Scherer
d136b05c1b Always evaluate free constants and statics, even if previous errors occurred 2024-02-19 22:11:13 +00:00
teor
6bc7c96bb3 cargo bless 2024-02-20 07:53:04 +10:00
teor
367a403367 Add test coverage for cast_sign_loss changes 2024-02-20 07:52:40 +10:00
teor
f40279ff7d Add some more test cases 2024-02-20 07:52:35 +10:00
teor
e74fe4362a Check for both signed and unsigned constant expressions 2024-02-20 07:51:21 +10:00
teor
47339d01f8 Bless fixed cast_sign_loss false negatives 2024-02-20 07:50:56 +10:00
Santiago Pastorino
16d5a2be3d Remove suspicious auto trait lint 2024-02-19 17:41:48 -03:00
roife
087c7c828d Add check for same guards in match_same_arms 2024-02-19 13:49:32 +00:00
Alex Macleod
1d107ab2be Remove $DIR replacement in test output 2024-02-17 12:34:54 +00:00
bors
5471e0645a Auto merge of #12305 - beetrees:asm-syntax, r=Manishearth
Ensure ASM syntax detect `global_asm!` and `asm!` only on x86 architectures

The ASM syntax lint is only relevant on x86 architectures, so this PR ensures it doesn't trigger on other architectures. This PR also makes the lints check `global_asm!` items as well as `asm!` expressions.

changelog: Check `global_asm!` items in the ASM syntax lints, and fix false positives on non-x86 architectures.
2024-02-17 01:54:24 +00:00