Commit graph

8050 commits

Author SHA1 Message Date
Samuel E. Moelius III
0bc26c811c needed_ref -> needs_ref 2022-08-21 19:38:09 +00:00
Samuel E. Moelius III
687fcf14c4 Fix to_string_in_format_args false positive 2022-08-21 19:38:09 +00:00
bors
cc637bacfa Auto merge of #9092 - tamaroning:fix-needless-match, r=llogiq
Fix false positives of needless_match

closes: #9084
made needless_match take into account arm in the form of `_ if => ...`

changelog: none
2022-08-21 13:22:21 +00:00
bors
e19a05cbb3 Auto merge of #8992 - kyoto7250:fix_8753, r=flip1995
feat(fix): Do not lint if the target code is inside a loop

close #8753

we consider the following code.

```rust
fn main() {
    let vec = vec![1];
    let w: Vec<usize> = vec.iter().map(|i| i * i).collect();  // <- once.

    for i in 0..2 {
        let _ = w.contains(&i);
    }
}
```

and the clippy will issue the following warning.

```rust
warning: avoid using `collect()` when not needed
 --> src/main.rs:3:51
  |
3 |     let w: Vec<usize> = vec.iter().map(|i| i * i).collect();
  |                                                   ^^^^^^^
...
6 |         let _ = w.contains(&i);
  |                 -------------- the iterator could be used here instead
  |
  = note: `#[warn(clippy::needless_collect)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect
help: check if the original Iterator contains an element instead of collecting then checking
  |
3 ~
4 |
5 |     for i in 0..2 {
6 ~         let _ = vec.iter().map(|i| i * i).any(|x| x == i);
```

Rewrite the code as indicated.

```rust
fn main() {
    let vec = vec![1];

    for i in 0..2 {
        let _ = vec.iter().map(|i| i * i).any(|x| x == i);  // <- execute `map` every loop.
    }
}
```

this code is valid in the compiler, but, it is different from the code before the rewrite.
So, we should not lint, If `collect` is outside of a loop.

Thank you in advance.

---

changelog: Do not lint if the target code is inside a loop in `needless_collect`
2022-08-21 09:58:24 +00:00
Philipp Krones
318ed05920
Reduce code duplication
Only check for the kind of loop once instead of re-desugaring it.
2022-08-21 11:03:54 +02:00
kyoto7250
5048af7a3a
feat(fix): Do not lint if the target code is inside a loop 2022-08-21 10:47:03 +02:00
Philipp Krones
1f75845a8f
Reduce indentation and add comment about lint name 2022-08-21 10:29:26 +02:00
J-ZhengLi
ffe7125163
and check for Result 2022-08-21 10:24:30 +02:00
J-ZhengLi
5d403c0b85
allow check for match in lint [option_if_let_else]
and add test case for `Result`
2022-08-21 10:24:27 +02:00
bors
5820addb24 Auto merge of #9269 - nahuakang:collapsible_str_replace, r=flip1995
Lint `collapsible_str_replace`

fixes #6651

```
changelog: [`collapsible_str_replace`]: create new lint `collapsible_str_replace`
```

If you added a new lint, here's a checklist for things that will be
checked during review or continuous integration.

- \[x] Followed [lint naming conventions][lint_naming]
- \[x] Added passing UI tests (including committed `.stderr` file)
- \[x] `cargo test` passes locally
- \[ ] Executed `cargo dev update_lints`
- \[x] Added lint documentation
- \[x] Run `cargo dev fmt`
2022-08-20 13:44:35 +00:00
Nahua Kang
b070b4045f Simplify lint logic and address code review comments 2022-08-20 12:09:09 +02:00
alex-semenyuk
2781ad0e9e Fix typos 2022-08-20 12:31:29 +03:00
bors
2091142f5d Auto merge of #9258 - Serial-ATA:unused-peekable, r=Alexendoo
Add [`unused_peekable`] lint

changelog: Add [`unused_peekable`] lint
closes: #854
2022-08-19 18:30:13 +00:00
Nahua Kang
fb30b64f63 Adjust test cases; run cargo dev bless 2022-08-19 20:00:20 +02:00
Nahua Kang
c989746ccf Remove checks on char slice; improve lint suggestion 2022-08-19 20:00:20 +02:00
Nahua Kang
a9bd0bd321 Handle repeated str::replace calls with single char kind to str 2022-08-19 20:00:19 +02:00
Nahua Kang
6e86687529 Handle replace calls with char slices 2022-08-19 19:55:28 +02:00
Nahua Kang
a4413f75bf Register new lint collapsible_str_replace to methods 2022-08-19 19:49:15 +02:00
bors
3a54117ffc Auto merge of #8804 - Jarcho:in_recursion, r=Alexendoo
Rework `only_used_in_recursion`

fixes #8782
fixes #8629
fixes #8560
fixes #8556

This is a complete rewrite of the lint. This loses some capabilities of the old implementation. Namely the ability to track through tuple and slice patterns, as well as the ability to trace through assignments.

The two reported bugs are fixed with this. One was caused by using the name of the method rather than resolving to the `DefId` of the called method. The second was cause by using the existence of a cycle in the dependency graph to determine whether the parameter was used in recursion even though there were other ways to create a cycle in the graph.

Implementation wise this switches from using a visitor to walking up the tree from every use of each parameter until it has been determined the parameter is used for something other than recursion. This is likely to perform better as it avoids walking the entire function a second time, and it is unlikely to walk up the HIR tree very much. Some cases would perform worse though.

cc `@buttercrab`

changelog: Scale back `only_used_in_recursion` to fix false positives
changelog: Move `only_used_in_recursion` back to `complexity`
2022-08-19 16:11:48 +00:00
Jason Newcomb
39f4bee98e Move only_used_in_recursion back into complexity 2022-08-19 11:42:14 -04:00
Jason Newcomb
d95b67560c Rework only_used_in_recursion 2022-08-19 11:42:14 -04:00
Alex Macleod
4f049f5a69 Refactor FormatArgsExpn 2022-08-19 15:35:26 +00:00
Jason Newcomb
d8808db006 Move VerboseFileReads into Methods lint pass 2022-08-19 10:54:55 -04:00
Jason Newcomb
8acc4d2f1e Move VecResizeToZero into Methods lint pass 2022-08-19 10:54:55 -04:00
Jason Newcomb
d8d4a135ea Move UnnecessarySortBy into Methods lint pass 2022-08-19 10:54:55 -04:00
Jason Newcomb
bb0584dfb4 Move UnitHash into Methods lint pass 2022-08-19 10:54:55 -04:00
Jason Newcomb
e213b6ee35 Move TransmutingNull into Transmute lint pass 2022-08-19 10:54:55 -04:00
Jason Newcomb
e834855950 Move StableSortPrimitive to Methods lint pass 2022-08-19 10:54:55 -04:00
Jason Newcomb
06d752e28d Move RepeatOnce into Methods lint pass 2022-08-19 10:54:55 -04:00
Jason Newcomb
fd5376194a Move range_zip_with_len into Methods lint pass 2022-08-19 10:54:55 -04:00
Jason Newcomb
226f135a03 Move PathBufPushOverwrite into Methods lint group 2022-08-19 10:32:31 -04:00
Jason Newcomb
0cc01cef30 Move OpenOptions into Methods lint pass 2022-08-19 10:32:16 -04:00
Jason Newcomb
508cf6bdbc Move MutMutexLock into Methods lint pass 2022-08-19 10:32:16 -04:00
Jason Newcomb
2f0ed0a0b1 Move MapErrIgnore into Methods lint pass 2022-08-19 10:32:15 -04:00
Jason Newcomb
452395485b Move MapClone into Methods lint pass 2022-08-19 10:31:41 -04:00
Jason Newcomb
5bc8813cdd Move ManualOkOr into Methods lint pass 2022-08-19 10:31:41 -04:00
Jason Newcomb
a8d80d531f Move GetFirst into Methods lint pass 2022-08-19 10:29:55 -04:00
Jason Newcomb
e3b77974d0 Move CaseSensitiveFileExtensionComparisons into Methods lint pass 2022-08-19 10:29:55 -04:00
Jason Newcomb
ba6a459528 Move BytesCountToLen into Methods lint pass 2022-08-19 10:28:59 -04:00
Jason Newcomb
2502898686 Move ByteCount into Methods lint pass 2022-08-19 10:28:59 -04:00
Jason Newcomb
21f595433e Move BorrowAsPtr into Casts lint pass 2022-08-19 10:24:58 -04:00
Jason Newcomb
8ab2f880d0 Move AsUnderscore into Casts lint pass 2022-08-19 10:23:19 -04:00
Serial
0efafa4a6e Better handle method/function calls 2022-08-19 08:18:34 -04:00
Serial
2666c38acb Add [unused_peekable] lint 2022-08-19 08:05:59 -04:00
bors
868dba9f65 Auto merge of #9295 - Guilherme-Vasconcelos:manual-empty-string-creation, r=dswij
Add `manual_empty_string_creations` lint

Closes #2972

- [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`

changelog: [`manual_empty_string_creations`]: Add lint for empty String not being created with `String::new()`
2022-08-19 11:19:06 +00:00
Lukas Lueg
e87a5a1cc5 Dont lint on match pattern-binding
Fixes #9347

Technically it is possible to have a blank match-pattern that does
nothing, and we fail to lint. But its easier to be safe than sorry here.
2022-08-18 19:30:56 +02:00
bors
c419d0a8b5 Auto merge of #9136 - smoelius:enhance-needless-borrow, r=Jarcho
Enhance `needless_borrow` to consider trait implementations

The proposed enhancement causes `needless_borrow` to suggest removing `&` from `&e` when `&e` is an argument position requiring trait implementations, and `e` implements the required traits. Example:
```
error: the borrowed expression implements the required traits
  --> $DIR/needless_borrow.rs:131:51
   |
LL |     let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap();
   |                                                   ^^^^^^^^^^^^^ help: change this to: `["-a", "-l"]`
```

r? `@Jarcho`

changelog: Enhance `needless_borrow` to consider trait implementations
2022-08-18 15:57:37 +00:00
bors
849c1c0465 Auto merge of #9338 - sgued:9331-unwrap-err-used, r=giraffate
unwrap_used and expect_used: trigger on uses of their _err variants

changelog: [`unwrap_used`]: lint uses of `unwrap_err`
changelog: [`expect_used`]: lint uses of `expect_err`

fixes #9331
2022-08-17 23:35:44 +00:00
Sosthène Guédon
ab91d5a540 unwrap_used: Fix error message for unwrap_err when expect_used is allowed 2022-08-17 18:58:17 +02:00
bors
84fb7e0395 Auto merge of #9287 - Jarcho:trans_undefined, r=xFrednet
`transmute_undefined_repr` fix

changelog: Don't lint `transmute_undefined_repr` when the the first field of a `repr(C)` type is compatible with the other type
2022-08-17 13:14:55 +00:00