Commit graph

9161 commits

Author SHA1 Message Date
Philipp Krones
5c1811ab94
Rename all clippy_config::msrvs -> clippy_utils::msrvs 2024-11-15 19:38:09 +01:00
Philipp Krones
c166ee1fc8
Merge remote-tracking branch 'upstream/master' into rustup 2024-11-14 18:27:35 +01:00
Jacherr
89210d7c5a new lint unnecessary_map_or 2024-11-12 23:00:26 +00:00
bors
e2962cc079 Auto merge of #126597 - estebank:unicode-output, r=fmease
Add Unicode block-drawing compiler output support

Add nightly-only theming support to rustc output using Unicode box
drawing characters instead of ASCII-art to draw the terminal UI.

In order to enable, the flags `-Zunstable-options=yes --error-format=human-unicode` must be passed in.

After:

```
error: foo
  ╭▸ test.rs:3:3
  │
3 │       X0 Y0 Z0
  │   ┌───╿──│──┘
  │  ┌│───│──┘
  │ ┏││━━━┙
  │ ┃││
4 │ ┃││   X1 Y1 Z1
5 │ ┃││   X2 Y2 Z2
  │ ┃│└────╿──│──┘ `Z` label
  │ ┃└─────│──┤
  │ ┗━━━━━━┥  `Y` is a good letter too
  │        `X` is a good letter
  ╰╴
note: bar
  ╭▸ test.rs:4:3
  │
4 │ ┏   X1 Y1 Z1
5 │ ┃   X2 Y2 Z2
6 │ ┃   X3 Y3 Z3
  │ ┗━━━━━━━━━━┛
  ├ note: bar
  ╰ note: baz
note: qux
  ╭▸ test.rs:4:3
  │
4 │   X1 Y1 Z1
  ╰╴  ━━━━━━━━
```

Before:

```
error: foo
 --> test.rs:3:3
  |
3 |       X0 Y0 Z0
  |    ___^__-__-
  |   |___|__|
  |  ||___|
  | |||
4 | |||   X1 Y1 Z1
5 | |||   X2 Y2 Z2
  | |||____^__-__- `Z` label
  | ||_____|__|
  | |______|  `Y` is a good letter too
  |        `X` is a good letter
  |
note: bar
 --> test.rs:4:3
  |
4 | /   X1 Y1 Z1
5 | |   X2 Y2 Z2
6 | |   X3 Y3 Z3
  | |__________^
  = note: bar
  = note: baz
note: qux
 --> test.rs:4:3
  |
4 |   X1 Y1 Z1
  |   ^^^^^^^^
```

After:

![rustc output with unicode box drawing characters](https://github.com/rust-lang/rust/assets/1606434/d210b79a-6579-4407-9706-ba8edc6e9f25)

Before:
![current rustc output with ASCII art](https://github.com/rust-lang/rust/assets/1606434/5aecccf8-a6ee-4469-8b39-72fb0d979a9f)
2024-11-11 00:00:58 +00:00
Esteban Küber
da93d78049 Add Unicode block-drawing compiler output support
Add nightly-only theming support to rustc output using Unicode box
drawing characters instead of ASCII-art to draw the terminal UI:

After:

```
error: foo
  ╭▸ test.rs:3:3
  │
3 │       X0 Y0 Z0
  │   ┌───╿──│──┘
  │  ┌│───│──┘
  │ ┏││━━━┙
  │ ┃││
4 │ ┃││   X1 Y1 Z1
5 │ ┃││   X2 Y2 Z2
  │ ┃│└────╿──│──┘ `Z` label
  │ ┃└─────│──┤
  │ ┗━━━━━━┥  `Y` is a good letter too
  │        `X` is a good letter
  ╰╴
note: bar
  ╭▸ test.rs:4:3
  │
4 │ ┏   X1 Y1 Z1
5 │ ┃   X2 Y2 Z2
6 │ ┃   X3 Y3 Z3
  │ ┗━━━━━━━━━━┛
  ├ note: bar
  ╰ note: baz
note: qux
  ╭▸ test.rs:4:3
  │
4 │   X1 Y1 Z1
  ╰╴  ━━━━━━━━
```

Before:

```
error: foo
 --> test.rs:3:3
  |
3 |       X0 Y0 Z0
  |    ___^__-__-
  |   |___|__|
  |  ||___|
  | |||
4 | |||   X1 Y1 Z1
5 | |||   X2 Y2 Z2
  | |||____^__-__- `Z` label
  | ||_____|__|
  | |______|  `Y` is a good letter too
  |        `X` is a good letter
  |
note: bar
 --> test.rs:4:3
  |
4 | /   X1 Y1 Z1
5 | |   X2 Y2 Z2
6 | |   X3 Y3 Z3
  | |__________^
  = note: bar
  = note: baz
note: qux
 --> test.rs:4:3
  |
4 |   X1 Y1 Z1
  |   ^^^^^^^^
```
2024-11-10 23:57:18 +01:00
Timo
f58088b23e
Add match-based manual try to clippy::question_mark (#13627)
Closes #10.

changelog: [`question_mark`]: Now lints for match-based manual try
2024-11-10 16:26:56 +00:00
Manish Goregaokar
4f0e46b74d
Fix large_include_file lint being triggered all the time by doc comments (#13672)
Fixes #13670.

Bug was that I forgot to add the comparison with the included file
content length...

changelog: Fix `large_include_file` lint being triggered all the time by
doc comments
2024-11-09 16:01:12 +00:00
Guillaume Gomez
223bffd0d2 Fix large_include_file lint being triggered all the time by doc comments 2024-11-09 13:55:20 +01:00
Jason Newcomb
8cfb95959b
fix: identity_op suggestions use correct parenthesis (#13647)
The `identity_op` lint was suggesting code fixes that resulted in
incorrect or broken code, due to missing parenthesis in the fix that
changed the semantics of the code.

For a binary expression, `left op right`, if the `left` was redundant,
it would check if the right side needed parenthesis, but if the `right`
was redundant, it would just assume that the left side did not need
parenthesis.

This can result in rustfix generating broken code and failing, or
generating code that has different behavior than before the fix. e.g.
`-(x + y + 0)` would turn into `-x + y`, changing the behavior, and
`1u64 + (x + y + 0i32) as u64` where `x: i32` and `y: i32` would turn
into `1u64 + x + y as u64`, creating an error where `x` cannot be added
to the other values, as it was never cast to `u64`.

This commit fixes both of these problems by always checking the
non-redundant child of a binary expression for needed parenthesis.

fixes #13470

changelog: [`identity_op`]: Fix suggested code that is broken or has
changed behavior
2024-11-09 05:03:10 +00:00
TheSlapstickDictator
ef0f1cad59 Remove check for duplicate parenthesis
It was an incorrect, and could lead to behavior changes in the
suggested code
2024-11-07 20:45:15 -08:00
Philipp Krones
6ced8c33c0 Merge commit 'f712eb5cdccd121d0569af12f20e6a0fabe4364d' into clippy-subtree-update 2024-11-07 22:37:01 +01:00
Philipp Krones
f712eb5cdc
Merge pull request #13657 from jdonszelmann/disallowed-macros-to-early
collect attribute spans early for disallowed macros
2024-11-07 19:23:20 +00:00
Philipp Krones
d583df02cc
Remove ui/no_lints.rs test
This test was only meant for debugging purposes
2024-11-07 17:27:46 +01:00
Philipp Krones
c64f1e3591
Fix lint_without_lint_pass internal lint 2024-11-07 17:27:46 +01:00
Philipp Krones
03daf7ccb2
Fix author lint and move it back to tests/ui
The author lint is not an internal lint, and should also be enabled, when Clippy
is distributed through rustup. This moves the author lint test cases back to
tests/ui.
2024-11-07 17:22:32 +01:00
Philipp Krones
b816d4ee4f
Merge remote-tracking branch 'upstream/master' into rustup 2024-11-07 17:22:32 +01:00
Samuel Tardieu
c1ab18d1c0 no_mangle_with_rust_abi: properly position the suggested ABI 2024-11-05 23:10:41 +01:00
Jonathan Dönszelmann
c93109674f
collect attribute spans early for disallowed macros 2024-11-05 20:46:44 +01:00
TheSlapstickDictator
1b7239d954 Fixing a missed check for needs_parenthesis in division 2024-11-04 19:19:58 -08:00
Jonathan Dönszelmann
63d0ba9de9 Move two attribute lints to be early pass (post expansion) 2024-11-04 22:47:22 +01:00
bors
ccf7c88386 Auto merge of #13648 - samueltardieu:push-rpxvoukpolvm, r=Centri3
needless_continue: check labels consistency before warning

changelog: [`needless_continue`]: check labels before warning about `continue` as the last statement in a loop body

Fix #13641
2024-11-03 23:17:36 +00:00
bors
a1a9aaef87 Auto merge of #13631 - samueltardieu:push-uoowutzkvsrk, r=Centri3
no_mangle attribute requires unsafe in Rust 2024

Tests without unsafe must not run in edition 2024. Also, error messages have been modified to include the full attribute, so that a use of `#[unsafe(no_mangle)]` does not produce an error message containing `#[no_mangle]`.

changelog: [`no_mangle_attribute`]: handle `#[unsafe(no_mangle)]` as well
2024-11-03 19:43:19 +00:00
Michael Goulet
f0cf9969cb Rename the FIXMEs, remove a few that dont matter anymore 2024-11-03 18:59:41 +00:00
TheSlapstickDictator
6d738f6bc0 Fix parens getting removed for non-associative operators 2024-11-03 09:23:03 -08:00
Samuel Tardieu
2f1b7b64fb needless_continue: check labels consistency before warning 2024-11-03 16:49:15 +01:00
bors
ddd1a86c66 Auto merge of #13573 - y21:issue13427, r=Centri3
Don't lint unnamed consts and nested items within functions in `missing_docs_in_private_items`

With this change we no longer require doc comments for `const _: ()` items as well as nested items in functions or other bodies. In both of those cases, rustdoc generates no documentation even with `--document-private-items`.

Fixes #13427 (first commit)
Fixes #13298 (second commit)
cc https://github.com/rust-lang/rust-clippy/issues/5736#issuecomment-668524296

changelog: [`missing_docs_in_private_items`]: avoid linting in more cases where rustdoc generates no documentation
2024-11-03 09:10:22 +00:00
TheSlapstickDictator
a0b7681a6f fix: identity_op suggestions use correct parenthesis
The `identity_op` lint was suggesting code fixes that resulted
in incorrect or broken code, due to missing parenthesis in the fix
that changed the semantics of the code.

For a binary expression, `left op right`, if the `left` was redundant,
it would check if the right side needed parenthesis, but if the `right`
was redundant, it would just assume that the left side did not need
parenthesis.

This can result in either rustfix generating broken code and failing,
or code that has different behavior than before the fix.
e.g. `-(x + y + 0)` would turn into `-x + y`, changing the behavior,
and `1u64 + (x + y + 0i32) as u64` where `x: i32` and `y: i32` would
turn into `1u64 + x + y as u64`, creating broken code where `x` cannot
be added to the other values, as it was never cast to `u64`.

This commit fixes both of these cases by always checking the
non-redundant child of a binary expression for needed parenthesis, and
makes it so if we need parenthesis, but they already exist, we don't add
any redundant ones.

Fixes #13470
2024-11-02 19:47:11 -07:00
GnomedDev
139bb25927 Avoid linting for closures with inferred return types 2024-11-02 13:41:16 +00:00
bors
5c6fe68c00 Auto merge of #13376 - decryphe:source-ordering, r=llogiq
new lint: `source_item_ordering`

changelog: [`source_item_ordering`]: Introduced a new restriction lint that checks the ordering of items in Modules, Enums, Structs, Impls and Traits.

From the written documentation:

> Why restrict this?
> Keeping a consistent ordering throughout the codebase helps with working as a team, and possibly improves maintainability of the codebase. The idea is that by defining a consistent and enforceable rule for how source files are structured, less time will be wasted during reviews on a topic that is (under most circumstances) not relevant to the logic implemented in the code. Sometimes this will be referred to as "bike-shedding".
>
> Keep in mind, that ordering source code alphabetically can lead to reduced performance in cases where the most commonly used enum variant isn't the first entry anymore, and similar optimizations that can reduce branch misses, cache locality and such. Either don't use this lint if that's relevant, or disable the lint in modules or items specifically where it matters. Other solutions can be to use profile guided optimization (PGO), or other advanced optimization methods.

I tried to build it as configurable as possible, as such a highly opinionated lint should be adjustable to personal opinions.

I'm open to any input and will be available both here and on the zulip for communication. In the meantime I'll be testing this lint against my own code-bases, which I've (manually) kept ordered with the default config, to see how well it works in practice.

And lastly, a big thanks to the community for making clippy the best linter there is!
2024-11-02 11:03:30 +00:00
GnomedDev
6205bcf0f1
Add match-based manual try to clippy::question_mark 2024-11-01 19:42:54 +00:00
bors
e4dc892ae3 Auto merge of #13608 - J-ZhengLi:issue12338-new, r=Alexendoo
[`infinite_loops`]: fix incorrect suggestions on async functions/closures

closes: #12338

I intend to fix this in #12421 but got distracted by some other problems in the same lint, delaying the process of closing the actual issue. So here's a separated PR that only focus on the issue and nothing else.

---

changelog: [`infinite_loops`]: fix suggestion error on async functions/closures
2024-11-01 19:36:02 +00:00
bors
8568ca8c23 Auto merge of #13634 - nyurik:fix-iter-without-into, r=xFrednet,samueltardieu
Cleanup code suggestion for `into_iter_without_iter`

Reorder the suggested code for the `IntoIterator` to match the ordering of the trait declaration:

```rust
impl IntoIterator for ... {
    type Item = ...;
    type IntoIter = ...;
```

changelog: none
2024-10-31 14:27:16 +00:00
bors
e1fa1b2c42 Auto merge of #13636 - GuillaumeGomez:extend-large_include_file, r=dswij
Extend `large_include_file` lint to also work on attributes

I realized randomly while working on another lint that `large_include_file` was not emitted on attributes. This PR fixes that.

changelog: Extend `large_include_file` lint to also work on attributes
2024-10-31 09:24:59 +00:00
bors
1998abcdce Auto merge of #13633 - chrysn-pull-requests:doc-valid-ident-coap, r=Centri3
Add 'CoAP' to doc-valid-idents

CoAP is a name of a network protocol common in embedded systems; one would talk in documentation about "a CoAP server" or "a CoAP client" without referring to a specific type.

This PR fixes false positives that arise from that use.

changelog: [`doc_markdown`]: Add CoAP to `doc-valid-idents`.

As this review is identical in structure to https://github.com/rust-lang/rust-clippy/pull/13460, I'm asking for a the same reviewer (if that works):

r? `@Centri3`
2024-10-30 21:58:56 +00:00
Guillaume Gomez
0c29fccf03 Extend large_include_file lint to also work on attributes 2024-10-30 20:41:34 +01:00
Yuri Astrakhan
323f144fe1 Cleanup code suggestion for into_iter_without_iter
Reorder the suggested code for the `IntoIterator` to match the ordering of the trait declaration:

```rust
impl IntoIterator for ... {
    type Item = ...;
    type IntoIter = ...;
```
2024-10-30 13:10:55 -04:00
Camille GILLOT
d9a0fec48a Remap impl-trait lifetimes on HIR instead of AST lowering. 2024-10-30 16:18:50 +00:00
bors
1bdc08a6bc Auto merge of #13599 - RuairidhWilliamson:proc_macro_attr, r=blyxyas
Fix allow_attributes when expanded from some macros

fixes #13349

The issue here was that the start pattern being matched on the original source code was not specific enough. When using derive macros or in the issue case a `#[repr(C)]` the `#` would match the start pattern meaning that the expanded macro appeared to be unchanged and clippy would lint it.

The change I made was to make the matching more specific by matching `#[ident` at the start. We still need the second string to match just the ident on its own because of things like `#[cfg_attr(panic = "unwind", allow(unused))]`.

I also noticed some typos with start and end, these code paths weren't being reached so this doesn't fix anything.

changelog: FP: [`allow_attributes`]: don't trigger when expanded from some macros
2024-10-30 15:22:47 +00:00
chrysn
bd4aa170ef Add 'CoAP' to doc-valid-idents 2024-10-30 12:57:24 +01:00
Samuel Tardieu
805245161a no_mangle attribute requires unsafe in Rust 2024 2024-10-30 11:31:14 +01:00
decryphe
f7ab2c9908 new lint: source_item_ordering 2024-10-30 10:03:16 +01:00
Robert Spencer
acc3842d43 Add new map_with_unused_argument_over_ranges lint
This lint checks for code that looks like
```rust
  let something : Vec<_> = (0..100).map(|_| {
    1 + 2 + 3
  }).collect();
```
which is more clear as
```rust
  let something : Vec<_> = std::iter::repeat_with(|| {
    1 + 2 + 3
  }).take(100).collect();
```
or
```rust
  let something : Vec<_> =
      std::iter::repeat_n(1 + 2 + 3, 100)
      .collect();
```

That is, a map over a range which does nothing with the parameter
passed to it is simply a function (or closure) being called `n`
times and could be more semantically expressed using `take`.
2024-10-29 21:32:00 +00:00
Samuel Tardieu
91a1d16a81 Add new lint: map_all_any_identity 2024-10-29 11:59:15 +01:00
bors
625d391107 Auto merge of #13437 - samueltardieu:issue-13434, r=y21
New lint `needless_as_bytes`

changelog: [`needless_as_bytes`]: new lint

Fix #13434
2024-10-29 08:03:05 +00:00
bors
149665afbd Auto merge of #128985 - GrigorenkoPV:instantly-dangling-pointer, r=Urgau
Lint against getting pointers from immediately dropped temporaries

Fixes #123613

## Changes:
1. New lint: `dangling_pointers_from_temporaries`. Is a generalization of `temporary_cstring_as_ptr` for more types and more ways to get a temporary.
2. `temporary_cstring_as_ptr` is removed and marked as renamed to `dangling_pointers_from_temporaries`.
3. `clippy::temporary_cstring_as_ptr` is marked as renamed to `dangling_pointers_from_temporaries`.
4. Fixed a false positive[^fp] for when the pointer is not actually dangling because of lifetime extension for function/method call arguments.
5. `core::cell::Cell` is now `rustc_diagnostic_item = "Cell"`

## Questions:
- [ ]  Instead of manually checking for a list of known methods and diagnostic items, maybe add some sort of annotation to those methods in library and check for the presence of that annotation? https://github.com/rust-lang/rust/pull/128985#issuecomment-2318714312

## Known limitations:

### False negatives[^fn]:

See the comments in `compiler/rustc_lint/src/dangling.rs`

1. Method calls that are not checked for:
   - `temporary_unsafe_cell.get()`
   - `temporary_sync_unsafe_cell.get()`
2. Ways to get a temporary that are not recognized:
   - `owning_temporary.field`
   - `owning_temporary[index]`
3. No checks for ref-to-ptr conversions:
   - `&raw [mut] temporary`
   - `&temporary as *(const|mut) _`
    - `ptr::from_ref(&temporary)` and friends

[^fn]: lint **should** be emitted, but **is not**

[^fp]: lint **should not** be emitted, but **is**
2024-10-29 00:24:07 +00:00
GnomedDev
c5df79d9db
Fire large_const_arrays for computed array lengths 2024-10-28 18:35:21 +00:00
bors
2e4a11ea77 Auto merge of #13338 - CoCo-Japan-pan:nonminimal_bool_casted, r=Centri3
fix incorrect suggestion for `!(a >= b) as i32 == c`

fixes #12761

The expression `!(a >= b) as i32 == c` got simplified to `a < b as i32 == c`, but this is a syntax error.
The result we want is `(a < b) as i32 == c`.
This is fixed by adding a parenthesis to the suggestion given in `check_simplify_not` when the boolean expression is casted.

changelog: [`nonminimal_bool`]: fix incorrect suggestion for `!(a >= b) as i32 == c`
2024-10-28 13:58:16 +00:00
Pavel Grigorenko
f286174690 New lint: dangling_pointers_from_temporaries 2024-10-28 14:16:05 +03:00
Matthias Krüger
303ab513d5 Rollup merge of #131391 - ChaiTRex:isqrt, r=scottmcm,tgross35
Stabilize `isqrt` feature

Stabilizes the `isqrt` feature. FCP is incomplete.

Closes #116226
2024-10-28 12:14:57 +01:00
CoCo-Japan-pan
d30a026a6b check if we need a parenthesis 2024-10-28 18:13:16 +09:00