Commit Graph

9554 Commits

Author SHA1 Message Date
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
Andre Bogus
13be95ab11 new manual_option_as_slice lint 2025-01-26 21:27:44 +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
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
06221e653c Deprecate the option_map_or_err_ok lint 2025-01-23 00:28:09 +01: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
Samuel Tardieu
f0b99b2b38 needless_option_take: add autofix 2025-01-20 11:53:08 +01:00
Samuel Tardieu
01907f77fc useless_conversion: use multipart suggestion to make adjustments more visible 2025-01-20 08:43:47 +01:00
lapla-cogito
7aae4f462a auto-fix for redundant_else lint 2025-01-20 08:13:01 +09:00
Timo
2280b8a099 Use clearer multipart suggestions for unnecessary_map_or lint (#13998)
A multipart suggestion will be used whenever the method call can be
replaced by another one with the first argument removed. It helps place
the new method call in context, especially when it is part of a larger
expression.

This fixes #13995 by applying a suggestion made by @y21.

r? @y21

changelog: [`unnecessary_map_or`]: better representation of suggestions
by placing them in context
2025-01-19 22:11:46 +00:00
Timo
0707fe8474 add a new lint for repeat().take() that can be replaced with repeat_n() (#13858)
close #13271

changelog: add new `manual_repeat_n` lint
2025-01-19 21:59:22 +00:00
Samuel Tardieu
1ccef58dc0 useless_conversion: add needed adjustments to suggestion 2025-01-19 22:42:38 +01:00
Samuel Tardieu
3a7f50f6d3 Apply unnecessary_semicolon to Clippy sources 2025-01-19 15:34:07 +01:00
Samuel Tardieu
51b0107d28 New lint: unnecessary_semicolon 2025-01-19 15:34:07 +01:00
Samuel Tardieu
27592e3ec8 Make unnecessary_map_or work with ref and Deref
Receivers which are references to `Option` and `Result`, or who
implement `Deref` to one of those types, will be linted as well.
2025-01-19 10:06:21 +01:00
Samuel Tardieu
7f37b2af97 Use clearer multipart suggestions for unnecessary_map_or lint
A multipart suggestion will be used whenever the method call can be
replaced by another one with the first argument removed. It helps place
the new method call in context, especially when it is part of a larger
expression.
2025-01-19 10:06:21 +01:00
Guillaume Gomez
277bf089b3 Fix regression #14007 2025-01-18 17:14:01 +01:00
Thomas Churchman
f327640706 Emit missing_const_for_fn for CONST_MUT_REFS 2025-01-18 13:31:04 +01:00
Samuel Tardieu
19ddb6922c Handle more cases in is_normalizable
By assuming that a recursive type is normalizable within the deeper
calls to `is_normalizable_helper()`, more cases can be handled by this
function.

In order to fix stack overflows, a recursion limit has also been added
for recursive generic type instantiations.
2025-01-18 05:43:07 +01:00
Samuel Tardieu
76bc88c40f More tests for large_enum_variant 2025-01-18 05:43:07 +01:00