Skip to content

Commit

Permalink
correct suggestion for map_with_unused_argument_over_ranges in a `n…
Browse files Browse the repository at this point in the history
…o_std` environment
  • Loading branch information
lapla-cogito committed Jan 27, 2025
1 parent d99eae4 commit 3b0b8b0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use clippy_utils::{eager_or_lazy, higher, usage};
use clippy_utils::{eager_or_lazy, higher, std_or_core, usage};
use rustc_ast::LitKind;
use rustc_ast::ast::RangeLimits;
use rustc_data_structures::packed::Pu128;
Expand Down Expand Up @@ -75,6 +75,7 @@ pub(super) fn check(
} = body_hir
&& !usage::BindingUsageFinder::are_params_used(cx, body_hir)
&& let Some(count) = extract_count_with_applicability(cx, range, &mut applicability)
&& let Some(exec_context) = std_or_core(cx)
{
let method_to_use_name;
let new_span;
Expand Down Expand Up @@ -105,7 +106,7 @@ pub(super) fn check(
let mut parts = vec![
(
receiver.span.to(method_call_span),
format!("std::iter::{method_to_use_name}"),
format!("{exec_context}::iter::{method_to_use_name}"),
),
new_span,
];
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/map_with_unused_argument_over_ranges_nostd.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![warn(clippy::map_with_unused_argument_over_ranges)]
#![no_std]
extern crate alloc;
use alloc::vec::Vec;

fn nostd(v: &mut [i32]) {
let _: Vec<_> = core::iter::repeat_n(3 + 1, 10).collect();
}
8 changes: 8 additions & 0 deletions tests/ui/map_with_unused_argument_over_ranges_nostd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![warn(clippy::map_with_unused_argument_over_ranges)]
#![no_std]
extern crate alloc;
use alloc::vec::Vec;

fn nostd(v: &mut [i32]) {
let _: Vec<_> = (0..10).map(|_| 3 + 1).collect();
}
15 changes: 15 additions & 0 deletions tests/ui/map_with_unused_argument_over_ranges_nostd.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: map of a closure that does not depend on its parameter over a range
--> tests/ui/map_with_unused_argument_over_ranges_nostd.rs:7:21
|
LL | let _: Vec<_> = (0..10).map(|_| 3 + 1).collect();
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::map-with-unused-argument-over-ranges` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::map_with_unused_argument_over_ranges)]`
help: remove the explicit range and use `repeat_n`
|
LL | let _: Vec<_> = core::iter::repeat_n(3 + 1, 10).collect();
| ~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~

error: aborting due to 1 previous error

0 comments on commit 3b0b8b0

Please sign in to comment.