Using `e.span.from_expansion()` alone prevented the lint from firing
within compiler-desugared expressions, e.g. `async fn`, `for` loops, or
`?`.
changelog: [`useless_conversion`]: also fire inside compiler desugarings
closerust-lang/rust-clippy#16590
rust-lang/rust-clippy#16535
This PR introduces a new configuration option, `allow-unwrap-types`,
which accepts a list of type paths (e.g., `["std::sync::LockResult"]`).
When configured, calling `.unwrap()` or `.expect()` on values of these
types will no longer trigger the `unwrap_used` or `expect_used` lints.
This is particularly useful for reducing noisy lints on patterns where
unwrapping is widely considered acceptable or expected by design, such
as obtaining a `std::sync::LockResult` from `Mutex::lock()`.
**Changes Made**
Introduced the `allow_unwrap_types: Vec<String>` field in
`clippy_config`.
Updated the `unwrap_expect_used` lint to check the underlying evaluated
expression `ty` against the configured allowed types.
Handled robust type alias resolution so that aliases like
`std::sync::LockResult` accurately map to their underlying `Adt` types
during lint evaluation.
Added UI tests in `tests/ui-toml/unwrap_used_allowed/` to verify both
allowed and disallowed cases.
----
changelog: [`unwrap_used`], [`expect_used`]: Add `allow-unwrap-types`
configuration to allow unwrapping specified types.
To be honest, the changes are confusing.
First of all, it has already been stated that it would be wrong if we
extended the lint to cover both sides.
https://github.com/rust-lang/rust-clippy/blob/18e5b2db4a3f9de20aa472d5c46a855e3a31d5c4/tests/ui/unnecessary_fold.rs#L91-L95
Secondly, there is some monstrous expression in the
`get_triggered_expr_span` function, and I think it could be rewritten
more cleanly.
Anyway, even if it has to be implemented, I'm glad to rewrite it more
thoroughly.
Fixesrust-lang/rust-clippy#16581.
----
changelog: [`unnecessary_fold`]: match against an accumulator on both
sides.
There is a proposal to change the behaviour of rustc's `must_use` lint
to consider `Result<T, U>` and `ControlFlow<U, T>` as `T` when `U` is
uninhabited. See <https://github.com/rust-lang/rust/pull/148214>.
This might make the user adding extra `#[must_use]` attributes to
functions returning `Result<T, !>` or `ControlFlow<!, T>`, which would
trigger the `double_must_use` lint in Clippy without the current change.
changelog: [`double_muse_use`, `drop_non_drop`,
`let_underscore_must_use`]: consider `Result<T, U>` and `ControlFlow<U,
T>` as `T` wrt the `#[must_use]` attribute if `U` is uninhabited.
There is a proposal to change the behaviour of rustc's `must_use` lint
to consider `Result<T, U>` and `ControlFlow<U, T>` as `T` when `U` is
uninhabited. See <https://github.com/rust-lang/rust/pull/148214>.
This might make the user adding extra `#[must_use]` attributes to
functions returning `Result<T, !>` or `ControlFlow<!, T>`, which would
trigger the `double_must_use` lint in Clippy without the current change.
Fixes: rust-lang/rust-clippy#16555
changelog: [unnecessary_min_or_max]: Fix false negatives for
usize::{MIN,MAX} by handling usize bit width correctly during constant
evaluation, enabling the lint to trigger on cases like n.min(0) /
n.max(usize::MAX).
DiagnosticCollector currently writes util/gh-pages/index.html, but the
doc comment still said util/gh-pages/lints.json.
The drift came from the switch to HTML generation in b522e7a94
("Generate lint list in HTML directly instead of JS"), which changed
the output file from lints.json to index.html but left this comment
unchanged.
Remove "failed to resolve" and use the same format we use in other resolution errors "cannot find `name`".
```
error[E0433]: cannot find `nonexistent` in `existent`
--> $DIR/custom_attr_multisegment_error.rs:5:13
|
LL | #[existent::nonexistent]
| ^^^^^^^^^^^ could not find `nonexistent` in `existent`
```
Fixesrust-lang/rust-clippy#16567.
Fix the compile-test tests when Cargo's `build.build-dir` is set to a
path that's distinct from `target-dir`. Right now, the uitest harness
assumes that `clippy-driver` (a final build artifact) is located in the
parent directory of `compile_test-…` (an intermediate artifact or,
rather, a dependency's final artifact), and that doesn't hold when
`build-dir` is configured independently from `target-dir`, like so (in
~/.cargo/config.toml):
```toml
[build]
build-dir = "{cargo-cache-home}/build-artifacts/{workspace-path-hash}"
```
changelog: none
We might not have the declaration around if `extern crate` is used, or
if rust-src is not installed for the standard library. Better err on the
safe side rather than having a behavior depending on what happens to be
available at compile time.
Fixesrust-lang/rust-clippy#14625
changelog: [`unnecessary_cast`]: do not warn on casts of external
function return type
Marking the `main` function as `#[must_use]` is nonsensical, as it is
not really supposed to be called anyway.
changelog: [`must_use_candidates`]: No longer lints `main` functions
with return values
Revert, but without type const.
Update symbol for feature err, then update suggestion output, and lastly update tests that change because of those.
Update these new tests with the correct syntax, and few existing tests with the new outputs the merge with main added.
Fix for tidyfmt and some errors when manually resolving a merge conflicts.
Update these tests to use update error messages and type const syntax.
Update comments and error message to use new syntax instead of old type_const attribute.
Remove the type_const attribute
update some more tests to use the new syntax.
Update these test cases.
update feature gate test
Change gate logic for `mgca_type_const_syntax` to work also if `min_generic_const_args` is enabled.
Create a new feature gate that checks for the feature before expansion.
Make rustfmt handle the `type const` syntax correctly.
Add a convience method to check if a RhsKind is type const.
Rename `Const` discriminant to `Body` for `ConstItemRhsKind`
Give the `TraitItemKind` flag an enum instead of a simple bool to better describe what the flag is for.
Update formatting for these match statements.
Update clippy test to use type const syntax.
Update test to use type const syntax.
update rustfmt to match ast items.
Update clippy to match ast and hir items.
Few more test cases that used old attribute, instead of 'type const'
Update to match the output from the feature gate checks.
tidyfmt adjustments.
Update the is_type_const, so I can constrain record!(..) in encoder.rs
Update conditional compilation test.
Move the feature gate to after expansion to allow for cfg(...) to work.
Update some more tests to use the new syntax.
Update type const tests in associated-const-bindings to use new syntax.
Don't check based off the attribute, but the item here.
Update some tests outside of the const_generics folder that were using #[type_const]
update the tests in associated consts that use #[type_const] to use type const
Update these mgca tests with the type const syntax.
Add a flag to TraitItemKind for detecting type const for now. Maybe later change ItemConstRhs to have optional consts but that touches a lot more lines of code.
Don't need into for these now that it's a query.
Add is_type_const query to handle foreign def ids.
update this test to use type const syntax.
Fix logic here, we only want to lower if there is expression in this case.
Update built-in macros to use ConstItemRhsKind
Update more instance of the old ConstItemRhs.
Rename ConstItemKind to ConstItemRhsKind, I noticed there is a typed called ConstantItemKind, so add the Rhs to the name to avoid confusion.
Update lower to use ConstItemKind
Add an other helper method to check if the rhs kinda has an expr.
Update item parse to use ConstItemKind enum.
Felt the field name could a be little clear when editing a few other things.
Change the ConstItem struct see know if we have a type const or regular const.
Make sure this syntax is properly feature gated.
Replace `ToString::to_string` with `ToOwned::to_owned` when the function is passed as is:
```rust
fn issue16511(x: Option<&str>) -> String {
// Replace with ToOwned::to_owned
x.map(ToString::to_string)
}
```
Now, only `call_span` is replaced, so the receiver is not a part of the
diff. This also removes the need to create a snippet for the receiver.
changelog: [`str_split`]: reduce suggestion diff
The [`allow_attributes`] lint false-negatived (failed to trigger) on
attributes that contained internal whitespace, such as `#[ allow (
dead_code ) ]`.
This happened because `clippy_utils::is_from_proc_macro` relied on
strict string matching (e.g., expecting exactly `#[allow`), which fails
if there are spaces.
#### Solution:
I have updated `clippy_utils::is_from_proc_macro` to support flexible
whitespace matching.
1. Added `Pat::Attr(Symbol)`.
2. Updated `span_matches_pat` to strip `#[` and trim validation
whitespace before checking the name.
Verified with a new test case in `tests/ui/allow_attributes.rs`.
Fixesrust-lang/rust-clippy#16491
----
changelog: [`allow_attributes`]: correctly detect attributes with
internal whitespace