Commit graph

7915 commits

Author SHA1 Message Date
Samuel Tardieu
aad3686823 Add error markers for obfuscated_if_else lint 2025-02-09 02:00:04 +01:00
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
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
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
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
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
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
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
4fef1b46be Fix tests 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
9e83183f7e uibless for tests 2025-01-25 18:42:45 +01:00
wowinter13
43066fea88 Fix related test 2025-01-25 18:42:45 +01:00
wowinter13
2ad20daa2a Rollback CHANGELOG.md change 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
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
Samuel Tardieu
69ff46ec9c arithmetic_side_effects: check adjusted expression types 2025-01-22 19:32:36 +01:00
llogiq
106db265f4
match_bool: fix suggestion if guard is present (#14039)
Without this check, the lint would suggest that

```rust
    match test {
        true if option == 5 => 10,
        _ => 1,
    };
```

is replaced by `if test { 10 } else { 1 }`.

changelog: [`match_bool`]: omit suggestion when guards are present in
`match` expression
2025-01-22 17:35:33 +00:00
Timo
c024c1327b
unnecessary_semicolon: do not lint if it may cause borrow errors (#14049)
Before edition 2024, some temporaries used in scrutinees of a `match`
used as the last expression of a block may outlive some referenced local
variables. Prevent those cases from happening by checking that alive
temporaries with significant drop do have a static lifetime.

The check is performed only for edition 2021 and earlier, and for the
last statement if it would become the last expression of the block.

changelog: [`unnecessary_semicolon`]: prevent borrow errors in editions
lower than 2024

r? @y21
2025-01-22 15:38:43 +00:00
Fridtjof Stoldt
67e6bf33fe
Suggest using Vec::extend() in same_item_push (#13987)
Using `Vec::extend(std::iter::repeat_n(item, N))` allows to use the more
natural number of elements to add `N`, as is probably done in the
original loop, instead of computing the difference between the existing
number of elements and the wanted one.

Before MSRV 1.82, the older suggestion to use `Vec::resize()` is still
issued.

Inspired by #6156 (which predates `repeat_n()`).

changelog: [`same_item_push`]: recommend using `Vec::extend()` to extend
a vector
2025-01-22 14:41:13 +00:00
Samuel Tardieu
9dca770aec unnecessary_semicolon: do not lint if it may cause borrow errors
Before edition 2024, some temporaries used in scrutinees in a `match`
used as the last expression of a block may outlive some referenced
local variables. Prevent those cases from happening by checking that
alive temporaries with significant drop do have a static lifetime.

The check is performed only for edition 2021 and earlier, and for the
last statement if it would become the last expression of the block.
2025-01-22 13:40:26 +01:00
Timo
88d83b6e55
short_circuit_statement: handle macros and parenthesis better (#14047)
- The lint no longer triggers if one of the operands in the boolean
expression comes from a macro expansion.
- Parenthesis are now removed inside the generated block if they are no
longer necessary.
- Error markers have been added.

changelog: [`short_circuit_statement`]: better handling of macros and
better looking suggestions
2025-01-22 02:54:49 +00:00
Timo
396de5748b
don't trigger needless_late_init when the first usage is in macro (#14053)
fix #13776

changelog: [`needless_late_init`]: don't trigger `needless_late_init`
when the first usage is in macro
2025-01-22 01:56:53 +00:00
lapla-cogito
26838f8552
don't trigger needless_late_init when the first usage is in macro 2025-01-22 10:44:53 +09:00
bors
d22dcb9cb4 Auto merge of #134299 - RalfJung:remove-start, r=compiler-errors
remove support for the (unstable) #[start] attribute

As explained by `@Noratrieb:`
`#[start]` should be deleted. It's nothing but an accidentally leaked implementation detail that's a not very useful mix between "portable" entrypoint logic and bad abstraction.

I think the way the stable user-facing entrypoint should work (and works today on stable) is pretty simple:
- `std`-using cross-platform programs should use `fn main()`. the compiler, together with `std`, will then ensure that code ends up at `main` (by having a platform-specific entrypoint that gets directed through `lang_start` in `std` to `main` - but that's just an implementation detail)
- `no_std` platform-specific programs should use `#![no_main]` and define their own platform-specific entrypoint symbol with `#[no_mangle]`, like `main`, `_start`, `WinMain` or `my_embedded_platform_wants_to_start_here`. most of them only support a single platform anyways, and need cfg for the different platform's ways of passing arguments or other things *anyways*

`#[start]` is in a super weird position of being neither of those two. It tries to pretend that it's cross-platform, but its signature is  a total lie. Those arguments are just stubbed out to zero on ~~Windows~~ wasm, for example. It also only handles the platform-specific entrypoints for a few platforms that are supported by `std`, like Windows or Unix-likes. `my_embedded_platform_wants_to_start_here` can't use it, and neither could a libc-less Linux program.
So we have an attribute that only works in some cases anyways, that has a signature that's a total lie (and a signature that, as I might want to add, has changed recently, and that I definitely would not be comfortable giving *any* stability guarantees on), and where there's a pretty easy way to get things working without it in the first place.

Note that this feature has **not** been RFCed in the first place.

*This comment was posted [in May](https://github.com/rust-lang/rust/issues/29633#issuecomment-2088596042) and so far nobody spoke up in that issue with a usecase that would require keeping the attribute.*

Closes https://github.com/rust-lang/rust/issues/29633

try-job: x86_64-gnu-nopt
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
try-job: test-various
2025-01-21 19:46:20 +00:00
Ralf Jung
759212cd59 remove support for the #[start] attribute 2025-01-21 06:59:15 -07:00
Samuel Tardieu
7f162fa9af short_circuit_statement: handle macros and parenthesis better
- The lint no longer triggers if the expression comes from macro
  expansion
- Parenthesis are now removed inside the generated block if they are no
  longer necessary.
2025-01-21 09:12:38 +01:00
Timo
d1b5fa2416
fix: correct suggestion for significant_drop_in_scrutinee in expressions (#14019)
This PR fixes an issue with the `significant_drop_in_scrutinee`, where
the lint generates invalid Rust syntax when suggesting fixes for match
expressions that are part of larger expressions, such as in assignment
contexts. For example:

```rust
    let mutex = Mutex::new(State {});
    let _ = match mutex.lock().unwrap().foo() {
        true => 0,
        false => 1,
    };
```
would suggest:
```rust
let _ = let value = mutex.lock().unwrap().foo();
match value {
```
With this PR, it now suggests:
```rust
let value = mutex.lock().unwrap().foo();
let _ = match value {
```

closes: #13986

changelog: [`significant_drop_in_scrutinee`] Fix incorrect suggestion
for `significant_drop_in_scrutinee` lint in expression context
2025-01-21 02:28:23 +00:00
Samuel Tardieu
0c3deeb246 match_bool: omit suggestion if guard is present
Without this check, the lint would suggest that

```rust
    match test {
        true if option == 5 => 10,
        _ => 1,
    };
```

is replaced by `if test { 10 } else { 1 }`.
2025-01-20 19:40:05 +01:00
llogiq
8f1b4bb87a
New lint: unnecessary_semicolon (#14032)
This lint detects and removes the unnecessary semicolon after a `match`
or `if` statement returning `()`. It seems to be quite a common
"mistake", given the number of hits (88) we had in the Clippy sources
themselves.

The lint doesn't bother about loops, as `rustfmt` already removes the
extra semicolon. It doesn't handle blocks either, as an extra block
level, followed or not by a semicolon, is likely intentional.

I propose to put the lint in `pedantic`, as putting it in `style` seems
quite hazardous given the number of hits.

Note: there exists a `redundant-semicolon` lint in the compiler, but it
is an early lint and cannot check that the expression evaluates to `()`,
so it ignores the cases we're handling here.

----

changelog: [`unnecessary_semicolon`]: new lint
2025-01-20 17:39:37 +00:00