Skip to content

Commit 3b0b8b0

Browse files
committed
correct suggestion for map_with_unused_argument_over_ranges in a no_std environment
1 parent d99eae4 commit 3b0b8b0

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

clippy_lints/src/methods/map_with_unused_argument_over_ranges.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::source::snippet_with_applicability;
55
use clippy_utils::sugg::Sugg;
6-
use clippy_utils::{eager_or_lazy, higher, usage};
6+
use clippy_utils::{eager_or_lazy, higher, std_or_core, usage};
77
use rustc_ast::LitKind;
88
use rustc_ast::ast::RangeLimits;
99
use rustc_data_structures::packed::Pu128;
@@ -75,6 +75,7 @@ pub(super) fn check(
7575
} = body_hir
7676
&& !usage::BindingUsageFinder::are_params_used(cx, body_hir)
7777
&& let Some(count) = extract_count_with_applicability(cx, range, &mut applicability)
78+
&& let Some(exec_context) = std_or_core(cx)
7879
{
7980
let method_to_use_name;
8081
let new_span;
@@ -105,7 +106,7 @@ pub(super) fn check(
105106
let mut parts = vec![
106107
(
107108
receiver.span.to(method_call_span),
108-
format!("std::iter::{method_to_use_name}"),
109+
format!("{exec_context}::iter::{method_to_use_name}"),
109110
),
110111
new_span,
111112
];
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![warn(clippy::map_with_unused_argument_over_ranges)]
2+
#![no_std]
3+
extern crate alloc;
4+
use alloc::vec::Vec;
5+
6+
fn nostd(v: &mut [i32]) {
7+
let _: Vec<_> = core::iter::repeat_n(3 + 1, 10).collect();
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![warn(clippy::map_with_unused_argument_over_ranges)]
2+
#![no_std]
3+
extern crate alloc;
4+
use alloc::vec::Vec;
5+
6+
fn nostd(v: &mut [i32]) {
7+
let _: Vec<_> = (0..10).map(|_| 3 + 1).collect();
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: map of a closure that does not depend on its parameter over a range
2+
--> tests/ui/map_with_unused_argument_over_ranges_nostd.rs:7:21
3+
|
4+
LL | let _: Vec<_> = (0..10).map(|_| 3 + 1).collect();
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::map-with-unused-argument-over-ranges` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::map_with_unused_argument_over_ranges)]`
9+
help: remove the explicit range and use `repeat_n`
10+
|
11+
LL | let _: Vec<_> = core::iter::repeat_n(3 + 1, 10).collect();
12+
| ~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)