Skip to content

redundant_closure makes incorrect suggestion #13895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lapla-cogito opened this issue Dec 29, 2024 · 1 comment · Fixed by #13999
Closed

redundant_closure makes incorrect suggestion #13895

lapla-cogito opened this issue Dec 29, 2024 · 1 comment · Fixed by #13999
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@lapla-cogito
Copy link
Contributor

lapla-cogito commented Dec 29, 2024

Summary

redundant_closure lint sometimes makes incorrect suggestion in a no_std environment.

Reproducer

I tried this code:

#![no_std]
extern crate alloc;
use alloc::vec;
use alloc::vec::Vec;

fn main() {
    let _: Option<Vec<u8>>= true.then(|| vec![]);
}

I expected to see this happen:

Suggest code that compiles successfully in a no_std environment, e.g. using alloc::vec::Vec, or not linting.

Instead, this happened:

The following was suggested, which fails to compile:

#![no_std]
extern crate alloc;
use alloc::vec;
use alloc::vec::Vec;

fn main() {
    let _: Option<Vec<u8>>= true.then(std::vec::Vec::new);
}

Version

rustc 1.85.0-nightly (dd84b7d5e 2024-12-27)
binary: rustc
commit-hash: dd84b7d5eec3c20d7fcd13e6450af029d3cece9d
commit-date: 2024-12-27
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Additional Labels

@rustbot label +I-suggestion-causes-error

@lapla-cogito lapla-cogito added the C-bug Category: Clippy is not doing the correct thing label Dec 29, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 29, 2024
@lapla-cogito
Copy link
Contributor Author

This part is the cause:

span_lint_and_sugg(
cx,
REDUNDANT_CLOSURE,
expr.span,
"redundant closure",
"replace the closure with `Vec::new`",
"std::vec::Vec::new".into(),
Applicability::MachineApplicable,
);

github-merge-queue bot pushed a commit that referenced this issue Jan 27, 2025
I opened #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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
2 participants