Commit graph

7386 commits

Author SHA1 Message Date
Jakub Beránek
e9852cc7c9
Only apply dereference-removing special case for actual references 2024-07-25 22:20:59 +02:00
bors
062b66d45c Auto merge of #12892 - meithecatte:needless-borrows-mutrefs, r=xFrednet
needless_borrows_for_generic_args: Fix for &mut

This commit fixes a bug introduced in #12706, where the behavior of the lint has been changed, to avoid suggestions that introduce a move. The motivation in the commit message is quite poor (if the detection for significant drops is not sufficient because it's not transitive, the proper fix would be to make it transitive). However, #12454, the linked issue, provides a good reason for the change — if the value being borrowed is bound to a variable, then moving it will only introduce friction into future refactorings.

Thus #12706 changes the logic so that the lint triggers if the value being borrowed is Copy, or is the result of a function call, simplifying the logic to the point where analysing "is this the only use of this value" isn't necessary.

However, said PR also introduces an undocumented carveout, where referents that themselves are mutable references are treated as Copy, to catch some cases that we do want to lint against. However, that is not sound — it's possible to consume a mutable reference by moving it.

To avoid emitting false suggestions, this PR reintroduces the referent_used_exactly_once logic and runs that check for referents that are themselves mutable references.

Thinking about the code shape of &mut x, where x: &mut T, raises the point that while removing the &mut outright won't work, the extra indirection is still undesirable, and perhaps instead we should suggest reborrowing: &mut *x. That, however, is left as possible future work.

Fixes #12856

changelog: none
2024-07-25 18:48:27 +00:00
Philipp Krones
9f53fc32cf
Merge remote-tracking branch 'upstream/master' into rustup 2024-07-25 18:23:27 +02:00
bors
5e6540f049 Auto merge of #13146 - Alexendoo:cast-lossless-128, r=y21
Lint casts to `u128` in `cast_lossless`

Reverts #12496 per https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Should.20.60as.20u128.60.20trigger.20cast_lossless

Also changes the lint messages and refactors the suggestion production - Fixes #12695

changelog: [`cast_lossless`]: lint casts to `u128`
2024-07-24 21:00:05 +00:00
Maja Kądziołka
e63061d75b
Apply review suggestion
Co-authored-by: Fridtjof Stoldt <xFrednet@gmail.com>
2024-07-24 22:29:35 +02:00
Alex Macleod
6d28e1a954 Lint casts to u128 in cast_lossless 2024-07-24 14:32:41 +00:00
bors
e91d82eabb Auto merge of #13143 - Alexendoo:transmute-ptr-to-ptr-suggestions, r=y21
Suggest `.cast`/`.cast_const`/`.cast_mut` in `transmute_ptr_as_ptr`

Essentially pre-applies `ptr_as_ptr` - https://github.com/rust-lang/rust-clippy/issues/6372#issuecomment-1830438907

changelog: none
2024-07-23 21:48:07 +00:00
Alex Macleod
7010d3c67e Don't suggest .cast_mut/.cast_const for types with erased lifetimes 2024-07-23 11:12:26 +00:00
bors
f9c240720a Auto merge of #13072 - Jarcho:misc_small5, r=xFrednet
Misc refactorings part 5

`toplevel_ref_arg` gets a small fix so it can be allowed on function arguments. Otherwise just some rearrangements.

changelog: none
2024-07-22 19:58:25 +00:00
Alex Macleod
afe811ad30 Suggest .cast/.cast_const/.cast_mut in transmute_ptr_as_ptr 2024-07-22 17:31:45 +00:00
Michael Goulet
897c4d3adb Move all error reporting into rustc_trait_selection 2024-07-21 22:34:35 -04:00
bors
1807580a49 Auto merge of #12772 - phi-gamma:redundant-struct-recreation, r=y21
add lint for recreation of an entire struct

This lint makes Clippy warn about situations where an owned struct is
essentially recreated by moving all its fields into a new instance of
the struct. The lint is not machine-applicable because the source
struct may have been partially moved.

This lint originated in something I spotted during peer review. While
working on their branch a colleague ended up with a commit where a
function returned a struct that 1:1 replicated one of its owned inputs
from its members. Initially I suspected they hadn’t run their code
through Clippy but AFAICS there is no lint for this situation yet.

changelog: new lint: [`redundant_owned_struct_recreation`]

### New lint checklist

- \[+] Followed [lint naming conventions][lint_naming]
- \[+] Added passing UI tests (including committed `.stderr` file)
- \[+] `cargo test` passes locally
- \[+] Executed `cargo dev update_lints`
- \[+] Added lint documentation
- \[+] Run `cargo dev fmt`
2024-07-21 23:02:38 +00:00
bors
8fe5c757d5 Auto merge of #11700 - lengyijun:pathbuf_join, r=xFrednet
[`pathbuf_init_then_push`]:  Checks for calls to `push` immediately a…

changelog: [`pathbuf_init_then_push`]: new lint: Checks for calls to `push` immediately after creating a new `PathBuf`

Just a mirror of VEC_INIT_THEN_PUSH
2024-07-21 06:39:01 +00:00
lengyijun
cb77f12600 [pathbuf_init_then_push]: Checks for calls to push immediately after creating a new PathBuf
Co-authored-by: Fridtjof Stoldt <xFrednet@gmail.com>
2024-07-21 14:22:48 +08:00
Bruce Mitchener
cf4270d13b Reduce typo count. 2024-07-20 12:50:36 +07:00
bors
bc2feea519 Auto merge of #13117 - GuillaumeGomez:fix-single_element_loop-suggestion, r=Alexendoo
Fix wrong suggestion for `single_element_loop` where parens were missing

Fixes https://github.com/rust-lang/rust-clippy/issues/12782.

changelog: Fix missing parens in `single_element_loop` suggestion
2024-07-18 12:53:59 +00:00
Guillaume Gomez
8dd310ddd7 Fix wrong suggestion for single_element_loop where parens were missing 2024-07-18 14:50:45 +02:00
Lzu Tao
3c50e7af5a Lint only if array length is a literal zero. 2024-07-18 12:22:08 +07:00
Lzu Tao
5540060d5d Skip linting if array length is from a macro. 2024-07-17 18:33:36 +07:00
Lzu Tao
c65ddb8728 Add regression test for issue 13110 2024-07-17 18:32:18 +07:00
Noah Lev
f2c1265483 Add ConstArgKind::Path and make ConstArg its own HIR node
This is a very large commit since a lot needs to be changed in order to
make the tests pass. The salient changes are:

- `ConstArgKind` gets a new `Path` variant, and all const params are now
  represented using it. Non-param paths still use `ConstArgKind::Anon`
  to prevent this change from getting too large, but they will soon use
  the `Path` variant too.

- `ConstArg` gets a distinct `hir_id` field and its own variant in
  `hir::Node`. This affected many parts of the compiler that expected
  the parent of an `AnonConst` to be the containing context (e.g., an
  array repeat expression). They have been changed to check the
  "grandparent" where necessary.

- Some `ast::AnonConst`s now have their `DefId`s created in
  rustc_ast_lowering rather than `DefCollector`. This is because in some
  cases they will end up becoming a `ConstArgKind::Path` instead, which
  has no `DefId`. We have to solve this in a hacky way where we guess
  whether the `AnonConst` could end up as a path const since we can't
  know for sure until after name resolution (`N` could refer to a free
  const or a nullary struct). If it has no chance as being a const
  param, then we create a `DefId` in `DefCollector` -- otherwise we
  decide during ast_lowering. This will have to be updated once all path
  consts use `ConstArgKind::Path`.

- We explicitly use `ConstArgHasType` for array lengths, rather than
  implicitly relying on anon const type feeding -- this is due to the
  addition of `ConstArgKind::Path`.

- Some tests have their outputs changed, but the changes are for the
  most part minor (including removing duplicate or almost-duplicate
  errors). One test now ICEs, but it is for an incomplete, unstable
  feature and is now tracked at #127009.
2024-07-16 19:27:28 -07:00
Michael Goulet
c1c945a350 Move rustc_infer::infer::error_reporting to rustc_infer::error_reporting::infer 2024-07-15 20:16:12 -04:00
apoisternex
489a77831c fix [excessive_precision] suggestions on float literal written in scientific notation
fixes: #12954

changelog: fix [`excessive_precision`] suggestions on float literal written in scientific notation
2024-07-15 18:52:18 -03:00
bors
eb4d88e690 Auto merge of #13102 - tesuji:fix-needless_option_as_deref, r=Alexendoo
Fix `needless_option_as_deref` false-positive on struct literals

changelog: [`needless_option_as_deref`] Fix false-positive on struct literals.

Fixes #13077 .

r?  Alexendoo
2024-07-15 16:23:41 +00:00
Lzu Tao
dcee2e8a0f Dont stop find loop node at struct field node 2024-07-15 15:56:17 +00:00
Lzu Tao
4264548283 Add regression test for issue 13077 2024-07-15 15:55:22 +00:00
Oli Scherer
3b390d416d Bump clippy version 2024-07-15 08:51:04 +00:00
bors
10ba798736 Auto merge of #13093 - waywardmonkeys:more-doc-valid-idents, r=Alexendoo
Add more doc-valid-idents

* "AccessKit" is a commonly used accessibility toolkit used in Rust GUIs.
* "CoreFoundation", "CoreGraphics", "CoreText" are frameworks on Apple OSes.
* "Direct2D", "Direct3D", "DirectWrite" are frameworks on Windows
* "PostScript" is a programming language and is mentioned when talking about text and vector graphics.
* "OpenAL" is an audio framework / API.
* "OpenType" is a font format (TrueType is already mentioned).
* "WebRTC", "WebSocket", "WebTransport" are web networking technologies.
* "NetBSD" and "OpenBSD" are like the already included FreeBSD.

changelog: [`doc_markdown`]: Add AccessKit, CoreFoundation, CoreGraphics, CoreText, Direct2D, Direct3D, DirectWrite, PostScript, OpenAL, OpenType, WebRTC, WebSocket, WebTransport, NetBSD, and OpenBSD to `doc-valid-idents`.
2024-07-14 14:09:20 +00:00
bors
e7d17e6cb2 Auto merge of #13061 - tesuji:fix-map-unwrap-or-13018, r=dswij
Fix `manual_unwrap_or` false positive

changelog: [`manual_unwrap_or`]: fix false positive of `if let Option<T>`

Closes #13018
2024-07-13 15:59:11 +00:00
J-ZhengLi
cc1bb8f57a make [or_fun_call] and [unwrap_or_default] recursive. 2024-07-13 23:15:40 +08:00
Bruce Mitchener
39378cf4f4 Add more doc-valid-idents
* "AccessKit" is a commonly used accessibility toolkit used in Rust GUIs.
* "CoreFoundation", "CoreGraphics", "CoreText" are frameworks on Apple OSes.
* "Direct2D", "Direct3D", "DirectWrite" are frameworks on Windows
* "PostScript" is a programming language and is mentioned when talking about
  text and vector graphics.
* "OpenAL" is an audio framework / API.
* "OpenType" is a font format (TrueType is already mentioned).
* "WebRTC", "WebSocket", "WebTransport" are web networking technologies.
* "NetBSD" and "OpenBSD" are like the already included FreeBSD.
2024-07-13 15:50:29 +07:00
J-ZhengLi
983b4c3720 [unwrap_or_default]: skip warning when calling inside of suggested method's implementation 2024-07-12 17:23:08 +08:00
Philipp Krones
c1fd25d0aa Merge commit 'b794b8e08c16517a941dc598bb1483e8e12a8592' into clippy-subtree-update 2024-07-11 15:44:03 +02:00
Philipp Krones
1ced73e66a
Merge remote-tracking branch 'upstream/master' into rustup 2024-07-11 15:29:08 +02:00
bors
ef3cfaa4f2 Auto merge of #13081 - GuillaumeGomez:fix-iter_next_loop.rs, r=y21
Fix `iter_next_loop.rs` ui test

I'm uncovering bugs while working on https://github.com/rust-lang/rust-clippy/pull/11421. ^^'

changelog: none
2024-07-10 20:26:03 +00:00
bors
8d3b1f9e30 Auto merge of #12984 - bitfield:fix_doc_nits_c, r=Alexendoo
Fix doc nits

More tender love and polish for the documentation and suggestion texts: adding formatting, links, full stops, tweaking wording for readability, changing 'which' to 'that' where appropriate, and other standard copyediting changes.

changelog: Docs [ `await_holding_lock` ]: fix doc nits
changelog: Docs [ `await_holding_refcell_ref` ]: fix doc nits
changelog: Docs [ `await_holding_invalid_type` ]: fix doc nits
changelog: Docs [ `cast_precision_loss` ]: fix doc nits
changelog: Docs [ `cast_sign_loss` ]: fix doc nits
changelog: Docs [ `cast_possible_truncation` ]: fix doc nits
changelog: Docs [ `cast_possible_wrap` ]: fix doc nits
changelog: Docs [ `cast_lossless` ]: fix doc nits
changelog: Docs [ `unnecessary_cast` ]: fix doc nits
changelog: Docs [ `cast_ptr_alignment` ]: fix doc nits
changelog: Docs [ `fn_to_numeric_cast` ]: fix doc nits
changelog: Docs [ `fn_to_numeric_cast_with_truncation` ]: fix doc nits
changelog: Docs [ `fn_to_numeric_cast_any` ]: fix doc nits
changelog: Docs [ `char_lit_as_u8` ]: fix doc nits
changelog: Docs [ `ptr_as_ptr` ]: fix doc nits
changelog: Docs [ `ptr_cast_constness` ]: fix doc nits
changelog: Docs [ `as_ptr_cast_mut` ]: fix doc nits
changelog: Docs [ `little_endian_bytes` ]: fix doc nits
changelog: Docs [ `big_endian_bytes` ]: fix doc nits
changelog: Docs [ `bind_instead_of_map` ]: fix doc nits
changelog: Docs [ `same_name_method` ]: fix doc nits
2024-07-10 17:55:08 +00:00
Guillaume Gomez
a987c308cd Fix iter_next_loop.rs ui test 2024-07-10 17:18:05 +02:00
bors
b012421501 Auto merge of #12944 - Jarcho:overflow_check, r=y21
Fix and rename `overflow_check_conditional`

fixes #2457

Other changes:
* Limit the lint to unsigned types.
* Actually check if the operands are the same rather than using only the first part of the path.
* Allow the repeated expression to be anything as long as there are no side effects.

changelog: Rename `overflow_check_conditional` to `panicking_overflow_check` and move to `correctness`
2024-07-10 09:02:11 +00:00
bors
ab7c910590 Auto merge of #13079 - sandersaares:u/sasaares/no-epsilon-guidance, r=y21
Fix guidance of [`float_cmp`] and [`float_cmp_const`] to not incorrectly recommend `f__::EPSILON` as the error margin.

Using `f32::EPSILON` or `f64::EPSILON` as the floating-point equality comparison error margin is incorrect, yet `float_cmp` has until now recommended this be done. This change fixes the given guidance (both in docs and compiler hints) to not reference these unsuitable constants.

Instead, the guidance now clarifies that the scenarios in which an absolute error margin is usable, provides a sample implementation for using a user-defined absolute error margin (as an absolute error margin can only be used-defined and may be different for different comparisons) and references the floating point guide for a reference implementation of relative error based equality comparison for cases where absolute error margins cannot be identified.

changelog: [`float_cmp`] Fix guidance to not incorrectly recommend `f__::EPSILON` as the error margin.
changelog: [`float_cmp_const`] Fix guidance to not incorrectly recommend `f__::EPSILON` as the error margin.

Fixes #6816
2024-07-10 06:36:10 +00:00
Philipp Gesang
296dcf09fb
unnecessary_struct_initialization: extend to assignments moving all fields
This lint makes Clippy warn about situations where an owned
struct is essentially recreated by moving all its fields into a
new instance of the struct. Until now this lint only triggered
for structs recreated from a base struct.

NB: The new functionality too will cause false positives for the
situation where a non-copy struct consisting of all copy members
is touched again in subsequent code.
2024-07-09 13:53:11 +02:00
Sander Saares
a067cd24ac Replace incorrect suggested fix for float_cmp
Using `f32::EPSILON` or `f64::EPSILON` as the floating-point equality comparison error margin is incorrect, yet `float_cmp` has until now recommended this be done. This change fixes the given guidance (both in docs and compiler hints) to not reference these unsuitable constants.

Instead, the guidance now clarifies that the scenarios in which an absolute error margin is usable, provides a reference implementation of using a user-defined absolute error margin (as an absolute error margin can only be used-defined and may be different for different comparisons) and references the floating point guide for a reference implementation of relative error based equaltiy comparison for when absolute error margin cannot be used.

changelog: Fix guidance of [`float_cmp`] and [`float_cmp_const`] to not incorrectly recommend `f64::EPSILON` as the error margin.

Fixes #6816
2024-07-09 10:12:09 +03:00
bors
dfb92532fa Auto merge of #12870 - lrh2000:sig-drop-while, r=blyxyas
`significant_drop_in_scrutinee`: Trigger lint also for scrutinees in `while let` and `if let`

This lint should also work for `if let` and `while let`, so this PR makes it actually work.

For `while let`, I can't think of any reason why this lint shouldn't be enabled. The only problem is that the lint suggests moving the significant drop above the `while let`, which is clearly invalid in the case of `while let`. I don't know if this is fixable, but this PR simply disables the wrong suggestions.

For `if let`, it seems that another lint called `if_let_mutex` has some overlapping functionality. But `significant_drop_in_scrutinee` is a bit stricter, as it will trigger even if the `else` branch does not try to lock the same mutex.

changelog: [`significant_drop_in_scrutinee`]: Trigger lint also for scrutinees in `while let` and `if let`.

r? `@blyxyas` (the third PR as promised in https://github.com/rust-lang/rust-clippy/pull/12740#issuecomment-2094876350, thanks for your review!)
2024-07-08 15:09:45 +00:00
bors
2ad8cdc81b Auto merge of #13045 - J-ZhengLi:missing_const_for_fn_FP, r=blyxyas
[`missing_const_for_fn`]: fix FP when arg ty is impl trait alias ty

closes: #13009

---

changelog: [`missing_const_for_fn`]: fix FP when arg ty is impl trait alias ty
2024-07-08 14:39:12 +00:00
J-ZhengLi
08992d0372 [missing_const_for_fn]: fix FP when arg ty is impl trait alias ty 2024-07-08 18:09:49 +08:00
Jason Newcomb
36a14e3a12 misc:
* Delay macro checks.
* Use `span_lint_hir`.
2024-07-07 22:05:26 -04:00
Jason Newcomb
5b7ffa1a63 Rework init_numbered_fields:
* Only check the name of a single field
* Don't lint in macros
* Check for side effects
* Don't use a binary heap
2024-07-07 20:15:19 -04:00
Jason Newcomb
23d96f65e4 Rename overflow_check_conditional to panicking_overflow_checks. 2024-07-07 10:24:42 -04:00
Jason Newcomb
aa371c37c2 Rewrite overflow_check_conditional 2024-07-07 10:22:29 -04:00
Lzu Tao
c46c1f6da6 Fix 13018: self should be T
with `Option::unwrap_or(self, T) -> T`.
2024-07-07 20:41:55 +07:00
John Arundel
f7050b0c78 resolve code review comments 2024-07-07 10:44:27 +01:00