Commit Graph

8109 Commits

Author SHA1 Message Date
bors
989ce17b55 Auto merge of #12125 - cocodery:issue12045, r=xFrednet
Fix error warning span for issue12045

fixes [Issue#12045](https://github.com/rust-lang/rust-clippy/issues/12045)

In issue#12045, unexpected warning span occurs on attribute `#[derive(typed_builder::TypedBuilder)]`, actually the warning should underline `_lifetime`.

In the source code we can find that the original intend is to warning on `ident.span`, but in this case, `stmt.span` is unequal with `ident.span`. So, fix the nit here is fine.

Besides, `ident.span` have an accurate range than `stmt.span`.

changelog: [`no_effect_underscore_binding`]: correct warning span
2024-01-19 13:54:06 +00:00
Samuel Tardieu
c6079a6880 blocks_in_conditions: do not warn if condition comes from macro 2024-01-19 01:06:08 +01:00
bors
4b3a9c09f3 Auto merge of #12167 - J-ZhengLi:issue12133, r=Alexendoo
fix FP on [`semicolon_if_nothing_returned`]

fixes: #12123

---

changelog: fix FP on [`semicolon_if_nothing_returned`] which suggesting adding semicolon after attr macro
2024-01-18 19:38:09 +00:00
y21
efd8dafa2f [default_numeric_fallback]: improve const context detection 2024-01-18 17:45:50 +01:00
J-ZhengLi
0e961cd854 fix suggestion error with attr macros 2024-01-18 18:53:41 +08:00
J-ZhengLi
9fe7c6a7ec finally came up with some repro code 2024-01-18 17:56:35 +08:00
bors
bb2d497364 Auto merge of #12146 - kristof-mattei:multiple-crate-versions-with-dashes, r=Manishearth
Fix [`multiple_crate_versions`] to correctly normalize package names to avoid missing the local one

Fixes #12145

changelog: [`multiple_crate_versions`]: correctly normalize package name
2024-01-17 23:00:36 +00:00
bors
2067fe482c Auto merge of #12155 - GuillaumeGomez:fix-9961, r=blyxyas
Correctly handle type relative in trait_duplication_in_bounds lint

Fixes #9961.

The generic bounds were not correctly checked and left out `QPath::TypeRelative`, making different bounds look the same and generating invalid errors (and fix).

r? `@blyxyas`

changelog: [`trait_duplication_in_bounds`]: Correctly handle type relative.
2024-01-17 15:44:16 +00:00
bors
e27ebf28e7 Auto merge of #11766 - dswij:issue-9274, r=blyxyas
`read_zero_byte_vec` refactor for better heuristics

Fixes #9274

Previously, the implementation of `read_zero_byte_vec` only checks for the next statement after the vec init. This fails when there is a block with statements that are expanded and walked by the old visitor.

This PR refactors so that:

1. It checks if there is a `resize`	on the vec
2. It works on blocks properly

e.g. This should properly lint now:

```
    let mut v = Vec::new();
    {
        f.read(&mut v)?;
        //~^ ERROR: reading zero byte data to `Vec`
    }
```

changelog: [`read_zero_byte_vec`] Refactored for better heuristics
2024-01-17 15:14:11 +00:00
bors
5f3a06023a Auto merge of #11608 - atwam:suspicious-open-options, r=y21
Add suspicious_open_options lint.

changelog: [`suspicious_open_options`]: Checks for the suspicious use of std::fs::OpenOptions::create() without an explicit OpenOptions::truncate().

create() alone will either create a new file or open an existing file. If the file already exists, it will be overwritten when written to, but the file will not be truncated by default. If less data is written to the file than it already contains, the remainder of the file will remain unchanged, and the end of the file will contain old data.

In most cases, one should either use `create_new` to ensure the file is created from scratch, or ensure `truncate` is called so that the truncation behaviour is explicit. `truncate(true)` will ensure the file is entirely overwritten with new data, whereas `truncate(false)` will explicitely keep the default behavior.

```rust
use std::fs::OpenOptions;

OpenOptions::new().create(true).truncate(true);
```

- [x] Followed [lint naming conventions][lint_naming]
- [x] Added passing UI tests (including committed `.stderr` file)
- [x] `cargo test` passes locally
- [x] Executed `cargo dev update_lints`
- [x] Added lint documentation
- [x] Run `cargo dev fmt`
2024-01-16 21:33:04 +00:00
Guillaume Gomez
7217c22f69 Update trait_duplication_in_bounds.rs ui test to add regression for ##9961 2024-01-16 12:13:27 +01:00
bors
ecb0311fcc Auto merge of #12149 - GuillaumeGomez:core-std-suggestions, r=llogiq
Correctly suggest std or core path depending if this is a `no_std` crate

A few lints emit suggestions using `std` paths whether or not this is a `no_std` crate, which is an issue when running `rustfix` afterwards. So in case this is an item that is defined in both `std` and `core`, we need to check if the crate is `no_std` to emit the right path.

r? `@llogiq`

changelog: Correctly suggest std or core path depending if this is a `no_std` crate
2024-01-16 06:26:29 +00:00
y21
f09cd88199
fix false positive in suspicious_open_options, make paths work 2024-01-15 17:15:09 +00:00
atwam
515fe65ba8
Fix conflicts
- New ineffective_open_options had to be fixed.
- Now not raising an issue on missing `truncate` when `append(true)`
  makes the intent clear.
- Try implementing more advanced tests for non-chained operations. Fail
2024-01-15 17:15:09 +00:00
atwam
84588a8815
Add suggestion/fix to suspicious_open_options
Also rebase and fix conflicts
2024-01-15 17:15:09 +00:00
atwam
2ec8729962
PR Fixes 2024-01-15 17:15:08 +00:00
atwam
6fb471d646
More helpful text, small style changes. 2024-01-15 17:15:08 +00:00
atwam
6c201db005
Add suspicious_open_options lint.
Checks for the suspicious use of OpenOptions::create()
without an explicit OpenOptions::truncate().

create() alone will either create a new file or open an
existing file. If the file already exists, it will be
overwritten when written to, but the file will not be
truncated by default. If less data is written to the file
than it already contains, the remainder of the file will
remain unchanged, and the end of the file will contain old
data.
In most cases, one should either use `create_new` to ensure
the file is created from scratch, or ensure `truncate` is
called so that the truncation behaviour is explicit.
`truncate(true)` will ensure the file is entirely overwritten
with new data, whereas `truncate(false)` will explicitely
keep the default behavior.

```rust
use std::fs::OpenOptions;

OpenOptions::new().create(true).truncate(true);
```
2024-01-15 17:15:08 +00:00
Ed Morley
a16a85030c
Add "OpenTelemetry" to default doc_valid_idents
The OpenTelemetry project's name is all one word (see https://opentelemetry.io),
so currently triggers a false positive in the `doc_markdown` lint.

The project is increasing rapidly in popularity, so it seems like a worthy
contender for inclusion in the default `doc_valid_idents` configuration.

I've also moved the existing "OpenDNS" entry earlier in the list, to restore
the alphabetical ordering of that "Open*" row.

The docs changes were generated using `cargo collect-metadata`.

changelog: [`doc_markdown`]: Add `OpenTelemetry` to the default configuration as an allowed identifier
2024-01-15 14:26:41 +00:00
bors
692f53fe8f Auto merge of #12136 - y21:issue12135, r=Jarcho
[`useless_asref`]: check that the clone receiver is the parameter

Fixes #12135

There was no check for the receiver of the `clone` call in the map closure. This makes sure that it's a path to the parameter.

changelog: [`useless_asref`]: check that the clone receiver is the closure parameter
2024-01-15 05:08:16 +00:00
bors
d6ff2d2c2d Auto merge of #12141 - samueltardieu:issue-12138, r=Jarcho
from_over_into: suggest a correct conversion to ()

changelog: [`from_over_into`]: suggest a correct conversion to `()`

Fix #12138
2024-01-15 04:32:22 +00:00
bors
a9fa2f5ed1 Auto merge of #12074 - ARandomDev99:12044-include-comments-while-checking-duplicate-code, r=Jarcho
Make `HirEqInterExpr::eq_block` take comments into account while checking if two blocks are equal

This PR:
- now makes `HirEqInterExpr::eq_block` take comments into account. Identical code with varying comments will no longer be considered equal.
- makes necessary adjustments to UI tests.

Closes #12044

**Lintcheck Changes**
- `match_same_arms` 53 => 52
- `if_same_then_else` 3 => 0

changelog: [`if_same_then_else`]: Blocks with different comments will no longer trigger this lint.
changelog: [`match_same_arms`]: Arms with different comments will no longer trigger this lint.
```
2024-01-15 04:06:52 +00:00
Guillaume Gomez
40a45a463f Update and add ui tests for core/std suggestions 2024-01-14 14:45:24 +01:00
Kristof Mattei
d975e267b9
fix: add fix for bug, fix test name 2024-01-13 17:44:27 -07:00
Kristof Mattei
5769c21f23
chore: negative test for multiple crate versions not working when name contains dashes 2024-01-13 17:27:43 -07:00
y21
be5707ce1b lint on .map(|&x| x.clone()) 2024-01-13 17:46:46 +01:00
Samuel Tardieu
e025356969 from_over_into: suggest a correct conversion to () 2024-01-13 13:19:55 +01:00
Guillaume Gomez
37947ffc40 Update ui tests for search_is_some lint 2024-01-12 22:48:30 +01:00
bors
7eca5afd34 Auto merge of #12137 - GuillaumeGomez:fix-unconditional_recursion-false-positive, r=llogiq
Fix false positive in `PartialEq` check in `unconditional_recursion` lint

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

We needed to check for the type of the previous element <del>in case it's a field</del>.

EDIT: After some extra thoughts, no need to check if it's a field, just if it's the same type as `Self`.

r? `@llogiq`

changelog: Fix false positive in `PartialEq` check in `unconditional_recursion` lint
2024-01-12 18:30:11 +00:00
Guillaume Gomez
132667288a Add regression ui test for unconditional_recursion lint on PartialEq 2024-01-12 17:37:09 +01:00
y21
153b83f61b [useless_asref]: check that the clone receiver is the local 2024-01-12 13:39:42 +01:00
bors
88b5d519a1 Auto merge of #12129 - GuillaumeGomez:map-clone-copy, r=llogiq
Fix suggestion for `map_clone` lint on types implementing `Copy`

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

It was missing this check to suggest the correct method.

r? `@llogiq`

changelog: Fix suggestion for `map_clone` lint on types implementing `Copy`
2024-01-11 23:28:12 +00:00
Guillaume Gomez
74db4b7f6d Add new ui tests for map_clone lint on types implementing Copy 2024-01-11 17:44:07 +01:00
Philipp Krones
2c0cea7cbc
Merge remote-tracking branch 'upstream/master' into rustup 2024-01-11 17:19:53 +01:00
cocodery
7c389acd27 Fix error warning span for issue12045 2024-01-11 14:52:03 +08:00
Guillaume Gomez
103e8881c6 Add tests to ensure that map_clone is not emitted if as_ref().clone() is present 2024-01-09 17:39:51 +01:00
Guillaume Gomez
8791a28c4a Also handle Result type for map_clone lint 2024-01-09 16:35:40 +01:00
Guillaume Gomez
cdd96bc662 Update ui tests for useless_asref lint extension 2024-01-09 14:14:29 +01:00
bors
3b8323d790 Auto merge of #12049 - cocodery:fix/issue#11243, r=Alexendoo
fix/issue#11243: allow 3-digit-grouped binary in non_octal_unix_permissions

fixes [Issue#11243](https://github.com/rust-lang/rust-clippy/issues/11243)

Issue#11243 suggest lint `non_octal_unix_permissions` should not report binary format literal unix permissions as an error, and we think binary format is a good way to understand these permissions.

To solve this problem, we need to add check for binary literal, which is written in function `check_binary_unix_permissions` , only `binary, 3 groups and each group length equals to 3` is a legal format.

changelog: [`non_octal_unix_permissions`]: Add check for binary format literal unix permissions like 0b111_111_111
2024-01-08 19:09:42 +00:00
bors
7fbaba856b Auto merge of #12080 - PartiallyTyped:12058, r=xFrednet
Fixed ICE introduced in #12004

Issue: in https://github.com/rust-lang/rust-clippy/pull/12004, we emit a lint for `filter(Option::is_some)`. If the
parent expression is a `.map` we don't emit that lint as there exists a
more specialized lint for that.

The ICE introduced in https://github.com/rust-lang/rust-clippy/pull/12004 is a consequence of the assumption that a
parent expression after a filter would be a method call with the filter
call being the receiver. However, it is entirely possible to have a
closure of the form

```
|| { vec![Some(1), None].into_iter().filter(Option::is_some) }
```
The previous implementation looked at the parent expression; namely the
closure, and tried to check the parameters by indexing [0] on an empty
list.

This commit is an overhaul of the lint with significantly more FP tests
and checks.

Impl details:

1. We verify that the filter method we are in is a proper trait method
   to avoid FPs.
2. We check that the parent expression is not a map by checking whether
   it exists; if is a trait method; and then a method call.
3. We check that we don't have comments in the span.
4. We verify that we are in an Iterator of Option and Result.
5. We check the contents of the filter.
   1. For closures we peel it. If it is not a single expression, we don't
     lint. We then try again by checking the peeled expression.
   2. For paths, we do a typecheck to avoid FPs for types that impl
     functions with the same names.
   3. For calls, we verify the type, via the path, and that the param of
     the closure is the single argument to the call.
   4. For method calls we verify that the receiver is the parameter of
     the closure. Since we handle single, non-block exprs, the
     parameter can't be shadowed, so no FP.

This commit also adds additional FP tests.

Fixes: #12058

Adding `@xFrednet` as you've the most context for this as you reviewed it last time.

`@rustbot` r? `@xFrednet`

---

changelog: none
(Will be backported and therefore don't effect stable)
2024-01-07 17:21:28 +00:00
Quinn Sinclair
bbadce9ec0 Fixed ICE introduced in #12004
Issue: in #12004, we emit a lint for `filter(Option::is_some)`. If the
parent expression is a `.map` we don't emit that lint as there exists a
more specialized lint for that.

The ICE introduced in #12004 is a consequence of the assumption that a
parent expression after a filter would be a method call with the filter
call being the receiver. However, it is entirely possible to have a
closure of the form

```
|| { vec![Some(1), None].into_iter().filter(Option::is_some) }
```
The previous implementation looked at the parent expression; namely the
closure, and tried to check the parameters by indexing [0] on an empty
list.

This commit is an overhaul of the lint with significantly more FP tests
and checks.

Impl details:

1. We verify that the filter method we are in is a proper trait method
   to avoid FPs.
2. We check that the parent expression is not a map by checking whether
   it exists; if is a trait method; and then a method call.
3. We check that we don't have comments in the span.
4. We verify that we are in an Iterator of Option and Result.
5. We check the contents of the filter.
  1. For closures we peel it. If it is not a single expression, we don't
     lint.
  2. For paths, we do a typecheck to avoid FPs for types that impl
     functions with the same names.
  3. For calls, we verify the type, via the path, and that the param of
     the closure is the single argument to the call.
  4. For method calls we verify that the receiver is the parameter of
     the closure. Since we handle single, non-block exprs, the
     parameter can't be shadowed, so no FP.

This commit also adds additional FP tests.
2024-01-07 17:11:34 +02:00
bors
f37e7f3585 Auto merge of #12109 - GuillaumeGomez:map-clone-call, r=llogiq
Handle "calls" inside the closure as well in `map_clone` lint

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

I just realized that I didn't handle the case where the `clone` method was made as a call and not a method call.

r? `@llogiq`

changelog: Handle "calls" inside the closure as well in `map_clone` lint
2024-01-07 14:23:02 +00:00
Guillaume Gomez
f66e940c88 Update ui test for map_clone lint 2024-01-07 13:24:52 +01:00
Samuel Tardieu
5b7a0de3e7 Do not suggest bool::then() and bool::then_some in const contexts 2024-01-07 10:43:18 +01:00
bors
0e5dc8e630 Auto merge of #11883 - J-ZhengLi:issue11642, r=dswij
improve [`cast_sign_loss`], to skip warning on always positive expressions

fixes: #11642

changelog: improve [`cast_sign_loss`] to skip warning on always positive expressions

Turns out this is change became quite big, and I still can't cover all the cases, like method calls such as `POSITIVE_NUM.mul(POSITIVE_NUM)`, or `NEGATIVE_NUM.div(NEGATIVE_NUM)`... but well, if I do, I'm scared that this will goes forever, so I stopped, unless it needs to be done, lol.
2024-01-06 19:22:59 +00:00
bors
788094c235 Auto merge of #11972 - samueltardieu:issue-11958, r=llogiq
Do not suggest `[T; n]` instead of `vec![T; n]` if `T` is not `Copy`

changelog: [`useless_vec`]: do not suggest replacing `&vec![T; N]` by `&[T; N]` if `T` is not `Copy`

Fix #11958
2024-01-06 18:56:30 +00:00
bors
17b2418208 Auto merge of #12104 - GuillaumeGomez:map-clone, r=llogiq
Extend `map_clone` lint to also work on non-explicit closures

I found it weird that this case was not handled by the current line so I added it. The only thing is that I don't see an obvious way to infer the current type to determine if it's copyable or not, so for now I always suggest `cloned` and I added a FIXME.

r? `@llogiq`

changelog: Extend `map_clone` lint to also work on non-explicit closures
2024-01-06 16:54:20 +00:00
Guillaume Gomez
6410606815 Update map_clone lint ui test 2024-01-06 17:22:21 +01:00
cocodery
60c647b262 Fix bug: allow no- '_'-split binary format string, add test 2024-01-06 21:41:25 +08:00
bors
5d57ba86a8 Auto merge of #12097 - y21:issue9427-3, r=llogiq
don't change eagerness for struct literal syntax with significant drop

Fixes the bug reported by `@ju1ius` in https://github.com/rust-lang/rust-clippy/issues/9427#issuecomment-1878428001.

`eager_or_lazy` already understands to suppress eagerness changes when the expression type has a significant drop impl, but only for initialization of tuple structs or unit structs. This changes it to also avoid changing it for `Self { .. }` and `TypeWithDrop { .. }`

changelog: [`unnecessary_lazy_eval`]: don't suggest changing eagerness for struct literal syntax when type has a significant drop impl
2024-01-05 20:25:18 +00:00