Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e146039

Browse files
committed
correct suggestion for repeat_vec_with_capacity in a no_std environment
1 parent 43b29da commit e146039

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

clippy_lints/src/repeat_vec_with_capacity.rs

Lines changed: 6 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::higher::VecArgs;
44
use clippy_utils::macros::matching_root_macro_call;
55
use clippy_utils::source::snippet;
6-
use clippy_utils::{expr_or_init, fn_def_id};
6+
use clippy_utils::{expr_or_init, fn_def_id, std_or_core};
77
use rustc_errors::Applicability;
88
use rustc_hir::{Expr, ExprKind};
99
use rustc_lint::{LateContext, LateLintPass};
@@ -93,14 +93,18 @@ fn check_repeat_fn(cx: &LateContext<'_>, expr: &Expr<'_>) {
9393
&& let ExprKind::Call(_, [repeat_expr]) = expr.kind
9494
&& fn_def_id(cx, repeat_expr).is_some_and(|did| cx.tcx.is_diagnostic_item(sym::vec_with_capacity, did))
9595
&& !repeat_expr.span.from_expansion()
96+
&& let Some(exec_context) = std_or_core(cx)
9697
{
9798
emit_lint(
9899
cx,
99100
expr.span,
100101
"iter::repeat",
101102
"none of the yielded `Vec`s will have the requested capacity",
102103
"if you intended to create an iterator that yields `Vec`s with an initial capacity, try",
103-
format!("std::iter::repeat_with(|| {})", snippet(cx, repeat_expr.span, "..")),
104+
format!(
105+
"{exec_context}::iter::repeat_with(|| {})",
106+
snippet(cx, repeat_expr.span, "..")
107+
),
104108
);
105109
}
106110
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![warn(clippy::repeat_vec_with_capacity)]
2+
#![allow(clippy::manual_repeat_n)]
3+
#![no_std]
4+
use core::iter;
5+
extern crate alloc;
6+
use alloc::vec::Vec;
7+
8+
fn nostd() {
9+
let _: Vec<Vec<u8>> = core::iter::repeat_with(|| Vec::with_capacity(42)).take(123).collect();
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![warn(clippy::repeat_vec_with_capacity)]
2+
#![allow(clippy::manual_repeat_n)]
3+
#![no_std]
4+
use core::iter;
5+
extern crate alloc;
6+
use alloc::vec::Vec;
7+
8+
fn nostd() {
9+
let _: Vec<Vec<u8>> = iter::repeat(Vec::with_capacity(42)).take(123).collect();
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: repeating `Vec::with_capacity` using `iter::repeat`, which does not retain capacity
2+
--> tests/ui/repeat_vec_with_capacity_nostd.rs:9:27
3+
|
4+
LL | let _: Vec<Vec<u8>> = iter::repeat(Vec::with_capacity(42)).take(123).collect();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: none of the yielded `Vec`s will have the requested capacity
8+
= note: `-D clippy::repeat-vec-with-capacity` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::repeat_vec_with_capacity)]`
10+
help: if you intended to create an iterator that yields `Vec`s with an initial capacity, try
11+
|
12+
LL | let _: Vec<Vec<u8>> = core::iter::repeat_with(|| Vec::with_capacity(42)).take(123).collect();
13+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
15+
error: aborting due to 1 previous error
16+

0 commit comments

Comments
 (0)