Commit graph

12506 commits

Author SHA1 Message Date
dswij
d79f86255d
Do not remove semicolon if it changes the block type (#14103)
Removing the semicolon of the last statement of an expressionless block
may change the block type even if the statement's type is `()`. If the
block type is `!` because of a systematic early return, typing it as
`()` may make it incompatible with the expected type for the block (to
which `!` is cast).

Fix #14100

changelog: [`unnecessary_semicolon`]: do not remove semicolon if it
could change the block type from `!` to `()`
2025-01-31 10:47:24 +00:00
Catherine Flores
e188c7d029
sliced_string_as_bytes: fix typo in lint description (#14117)
changelog: none
2025-01-30 20:55:55 +00:00
llogiq
398a5c2db9
New lint: precedence_bits, with recent additions to precedence (#14115)
Commit 2550530266 has extended the
`precedence` lint to include bitmasking and shift operations. The lint
is warn by default, and this generates many hits, especially in embedded
or system code, where it is very idiomatic to use expressions such as `1
<< 3 | 1 << 5` without parentheses.

This commit splits the recent addition into a new lint, which is put
into the "restriction" category, while the original one stays in
"complexity", because mixing bitmasking and arithmetic operations is
less typical.

Fix #14097

changelog: [`precedence_bits`]: new lint
2025-01-30 20:39:07 +00:00
Samuel Tardieu
5aa8bc11cd sliced_string_as_bytes: fix typo in lint description 2025-01-30 21:27:57 +01:00
Catherine Flores
f51e18de30
feat: new lint for and_then when returning Option or Result (#14051)
close #6436

changelog: [`return_and_then`]: new lint
2025-01-30 20:14:48 +00:00
Samuel Tardieu
8db9ecfd74 New lint: precedence_bits, with recent additions to precedence
Commit 2550530266 has extended the
`precedence` lint to include bitmasking and shift operations. The lint
is warn by default, and this generates many hits, especially in embedded
or system code, where it is very idiomatic to use expressions such as
`1 << 3 | 1 << 5` without parentheses.

This commit splits the recent addition into a new lint, which is put
into the "restriction" category, while the original one stays in
"complexity", because mixing bitmasking and arithmetic operations is
less typical.
2025-01-30 17:07:50 +01:00
llogiq
ad05bc055c
Include generic arguments when suggesting a closure η-reduction (#14105)
Fix #14096

changelog: [`redundant_closure_for_method_calls`]: repeat generic args
in suggestion
2025-01-30 05:15:35 +00:00
Alejandra González
88a00a87fa
autofix for redundant_else lint (#13936)
changelog: [`redundant_else`]: autofix for some cases

`redundant_else` can be fixed automatically.
2025-01-30 00:01:31 +00:00
Samuel Tardieu
e4505fb670 Include generic arguments when suggesting a closure η-reduction 2025-01-29 23:59:23 +01:00
Samuel Tardieu
78d6b2ea4e Do not remove semicolon if it changes the block type
Removing the semicolon of the last statement of an expressionless block
may change the block type even if the statement's type is `()`. If the
block type is `!` because of a systematic early return, typing it as
`()` may make it incompatible with the expected type for the block (to
which `!` is cast).
2025-01-29 23:01:08 +01:00
Aaron Ang
84fb6b1651 Add new lint return_and_then 2025-01-29 10:43:59 -08:00
Catherine Flores
e02c8857e8
Move format_push_string and format_collect to pedantic (#13894)
Closes #11434 by moving `format_push_string` and `format_collect` to
pedantic.

changelog: Move `format_push_string` and `format_collect` to pedantic
2025-01-28 19:32:24 +00:00
Catherine Flores
6a209cdc90
Fix escaping problem in write_literal and print_literal lint suggestion (#13990)
fix #13959

The current implementation of the `write_literal` and `print_literal`
lint performs escaping for the second argument of `write!` ,`writeln!`,
`print!` and `println!` of the suggestion by first replacing `"` with
`\"`, and then replacing `\` with `\\`. Performing these replacements in
this order may lead to unnecessary backslashes being added if the
original code contains `"` (e.g. `"` -> `\\"`), potentially resulting in
a suggestion that causes the code to fail to compile.
In the issue mentioned above, it’s suggested to use raw strings as raw
strings, but implementing this would require an ad-hoc change to the
current implementation, so it has been deferred. (I'll implement this in
another PR)

changelog: [`write_literal`]: fix incorrect escaping of suggestions
changelog: [`print_literal`]: fix incorrect escaping of suggestions
2025-01-28 19:29:57 +00:00
llogiq
9ede32fe00
needless_option_take: add autofix (#14042)
changelog: [`needless_option_take`]: add autofix
2025-01-28 18:49:27 +00:00
Philipp Krones
145d5adf04
Merge remote-tracking branch 'upstream/master' into rustup 2025-01-28 19:14:45 +01:00
Yutaro Ohno
4693d0a9ff Add new lint doc_overindented_list_items
Add a new lint `doc_overindented_list_items` to detect and fix list items
in docs that are overindented.

For example,

```rs
/// - first line
///      second line
fn foo() {}
```

this would be fixed to:

```rs
/// - first line
///   second line
fn foo() {}
```

This lint improves readabiliy and consistency in doc.
2025-01-28 16:22:28 +09:00
Catherine Flores
66dc8a1a30
proper applicability for obfuscated_if_else (#14061)
fix #14034

The currect implementation of `obfuscated_if_else` sometimes makes
incorrect suggestions when the original code have side effects (see the
example in the above issue). I think this can be fixed by changing the
applicability depending on whether it can have side effects or not.

changelog: [`obfuscated_if_else`]: change applicability when the
original code can have side effects
2025-01-28 06:57:12 +00:00
Manish Goregaokar
83bde363b6
correct suggestions in no_std (#13999)
I opened https://github.com/rust-lang/rust-clippy/pull/13896 before.
However, I found that there're more cases where Clippy suggests to use
modules that belong to the `std` crate even in a `no_std` environment.
Therefore, this PR include the changes I've made in #13896 and new
changes to fix cases I found this time to prevent wrong suggestions in
`no_std` environments as well.

changelog: [`redundant_closure`]: correct suggestion in `no_std`
changelog: [`repeat_vec_with_capacity`]: correct suggestion in `no_std`
changelog: [`single_range_in_vec_init`]: don't emit suggestion to use
`Vec` in `no_std`
changelog: [`drain_collect`]: correct suggestion in `no_std`
changelog: [`map_with_unused_argument_over_ranges`]: correct suggestion
in `no_std`

also close #13895
2025-01-27 21:36:12 +00:00
lapla-cogito
3b0b8b080e
correct suggestion for map_with_unused_argument_over_ranges in a no_std environment 2025-01-28 06:29:47 +09:00
lapla-cogito
d99eae4325
correct suggestion for drain_collect in a no_std environment 2025-01-28 06:29:45 +09:00
lapla-cogito
cb0a479d1f
don't suggest to use std::vec::Vec for single_range_in_vec_init in a no_std environment 2025-01-28 06:29:45 +09:00
lapla-cogito
e146039e17
correct suggestion for repeat_vec_with_capacity in a no_std environment 2025-01-28 06:29:37 +09:00
lapla-cogito
43b29da91e
correct suggestion for redundant_closure in a no_std environment 2025-01-28 06:24:44 +09:00
Manish Goregaokar
85bbba69b0
New lint sliced_string_as_bytes (#14002)
resurrection of https://github.com/rust-lang/rust-clippy/pull/10984

fixes https://github.com/rust-lang/rust-clippy/issues/10981

changelog: [`sliced_string_as_bytes`]: add new lint
`sliced_string_as_bytes`
2025-01-27 20:34:48 +00:00
Manish Goregaokar
ac805d4a4e
Remove "Known problems" section for borrow_interior_mutable_const (#14078)
Remove "Known problems" section for `borrow_interior_mutable_const`
since issue were fixed some time ago - #6110, #5812

changelog: none
2025-01-27 05:14:57 +00:00
León Orell Valerian Liehr
c5196736b2 Rollup merge of #126604 - kadiwa4:uplift_double_negation, r=nnethercote
Uplift `clippy::double_neg` lint as `double_negations`

Warns about cases like this:
```rust
fn main() {
    let x = 1;
    let _b = --x; //~ WARN use of a double negation
}
```

The intent is to keep people from thinking that `--x` is a prefix decrement operator. `++x`, `x++` and `x--` are invalid expressions and already have a helpful diagnostic.

I didn't add a machine-applicable suggestion to the lint because it's not entirely clear what the programmer was trying to achieve with the `--x` operation. The code that triggers the lint should always be reviewed manually.

Closes #82987
2025-01-27 04:34:50 +01:00
Alejandra González
057f5b36f5
Make unnecessary_map_or work with ref and Deref to Option/Result (#14024)
Receivers which are references to `Option` and `Result`, or who
implement `Deref` to one of those types, will be linted as well.

changelog: [`unnecessary_map_or`]: work with ref and `Deref` to `Option`
and `Result` as well

Fixes #14023

**Note:** this patch must be merged after #13998 – only the second
commit must be reviewed, the first one repeats the patch in #13998 for
mergeability reasons.
2025-01-26 18:06:49 +00:00
Kalle Wachsmuth
25a77cf4f4 remove clippy::double_neg 2025-01-26 12:15:12 +01:00
bors
500614552d Auto merge of #135753 - compiler-errors:from-ty-const, r=oli-obk
Get rid of `mir::Const::from_ty_const`

This function is strange, because it turns valtrees into `mir::Const::Value`, but the rest of the const variants stay as type system consts.

All of the callsites except for one in `instsimplify` (array length simplification of `ptr_metadata` call) just go through the valtree arm of the function, so it's easier to just create a `mir::Const` directly for those.

For the instsimplify case, if we have a type system const we should *keep* having a type system const, rather than turning it into a `mir::Const::Value`; it doesn't really matter in practice, though, bc `usize` has no padding, but it feels more principled.
2025-01-26 09:26:34 +00:00
alexey semenyuk
ba78c227dc
Remove "Known problems" section for borrow_interior_mutable_const 2025-01-26 13:52:52 +05:00
lapla-cogito
2640f657ec
change the applicability of obfuscated_if_else depending on whether the original code can have side effects 2025-01-26 10:03:20 +09:00
llogiq
913592373d
trigger obfuscated_if_else for .then(..).unwrap_or(..) (#14021)
part of https://github.com/rust-lang/rust-clippy/issues/9100

The `obfuscated_if_else` lint currently only triggers for the pattern
`.then_some(..).unwrap_or(..)`, but there're other cases where this lint
should be triggered, one of which is `.then(..).unwrap_or(..)`.

changelog: [`obfuscated_if_else`]: trigger lint for the
`.then(..).unwrap_or(..)` pattern as well
2025-01-25 23:48:02 +00:00
wowinter13
beeb6f7432 slice-as-bytes: pedantic -> perf 2025-01-25 18:43:07 +01:00
wowinter13
4e94d22291 nit: change placeholders 2025-01-25 18:43:07 +01:00
wowinter13
5855e0a2f1 Small refactoring: irrefutable let pattern 2025-01-25 18:43:07 +01:00
wowinter13
26a7b322a3 Rename slice_as_bytes -> sliced_string_as_bytes 2025-01-25 18:43:07 +01:00
wowinter13
7124d822b4 Fix function signature 2025-01-25 18:42:45 +01:00
wowinter13
307d85409b Address linter & changelog issues 2025-01-25 18:42:45 +01:00
wowinter13
d8752dbf40 New lint 2025-01-25 18:42:43 +01:00
lapla-cogito
db977689fd
trigger obfuscated_if_else for .then(..).unwrap_or(..) 2025-01-25 19:57:22 +09:00
J-ZhengLi
2671e4a5c1 add new lint non_std_lazy_statics
detect usage of `once_cell::sync::Lazy` and `lazy_static!`,
recommending usage of `std::sync::LazyLock` instead
2025-01-24 14:06:46 -07:00
Alex Macleod
d7e20a9c16
docs: fix verbose-bit-mask example (#14055)
changelog: none

`x & 15 == 0` is not equivalent to `x.trailing_zeros() > 4`, as `x =
0b10000` is true for the former and false for the latter.

In fact, clippy itself suggests the following:

```rust
pub fn src(x: i32) -> bool {
    x & 15 == 0 // ~error: bit mask could be simplified with a call to `trailing_zeros`
    ^^^^^^^^^^^ help: try: `x.trailing_zeros() >= 4`
}
```
2025-01-24 15:48:44 +00:00
Timo
4b05f50b6b
Add necessary adjustments to suggestion to remove redundant .into_iter() calls (#14035)
Fix #11819

changelog: [`useless_conversion`]: add necessary adjustments to
suggestion to remove redundant `.into_iter()` calls
2025-01-23 13:35:52 +00:00
Catherine Flores
92fac5c64c
clarify message for non-obvious precedence (#14043)
I ran across this message while writing code and thought it was quite
odd. I've simplified it and hopefully made it clearer for non-native
English speakers.

changelog: clarify message for non-obvious precedence
2025-01-23 09:31:51 +00:00
Jacob Pratt
2f2d09a858
clarify message for non-obvious precedence 2025-01-23 01:22:26 -05:00
Boxy
3309f0296f make hir::Ty/ConstArg methods generic where applicable 2025-01-23 06:01:36 +00:00
Boxy
6ed958869d visit_x_unambig 2025-01-23 06:01:36 +00:00
Boxy
109440b830 The clipper :3c 2025-01-23 06:01:36 +00:00
Boxy
5c4e9401dc Make hir::TyKind::TraitObject use tagged ptr 2025-01-23 06:01:36 +00:00
Catherine Flores
2c8d1ae592
Fix out-of-date comment (#14040)
This comment was left behind when the method receiver was split out from
the method arguments in 4bcaddeeb2.

changelog: none
2025-01-22 22:32:33 +00:00