-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
new lint: repeat_vec_with_capacity
#11597
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use clippy_utils::consts::{constant, Constant}; | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::higher::VecArgs; | ||
use clippy_utils::macros::root_macro_call; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::{expr_or_init, fn_def_id, match_def_path, paths}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::{sym, Span}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Looks for patterns such as `vec![Vec::with_capacity(x); n]` or `iter::repeat(Vec::with_capacity(x))`. | ||
/// | ||
/// ### Why is this bad? | ||
/// These constructs work by cloning the element, but cloning a `Vec<_>` does not | ||
/// respect the old vector's capacity and effectively discards it. | ||
/// | ||
/// This makes `iter::repeat(Vec::with_capacity(x))` especially suspicious because the user most certainly | ||
/// expected that the yielded `Vec<_>` will have the requested capacity, otherwise one can simply write | ||
/// `iter::repeat(Vec::new())` instead and it will have the same effect. | ||
y21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// Similarly for `vec![x; n]`, the element `x` is cloned to fill the vec. | ||
/// Unlike `iter::repeat` however, the vec repeat macro does not have to clone the value `n` times | ||
/// but just `n - 1` times, because it can reuse the passed value for the last slot. | ||
/// That means that the last `Vec<_>` gets the requested capacity but all other ones do not. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// # use std::iter; | ||
/// | ||
/// let _: Vec<Vec<u8>> = vec![Vec::with_capacity(42); 123]; | ||
y21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// let _: Vec<Vec<u8>> = iter::repeat(Vec::with_capacity(42)).take(123).collect(); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// # use std::iter; | ||
/// | ||
/// let _: Vec<Vec<u8>> = iter::repeat_with(|| Vec::with_capacity(42)).take(123).collect(); | ||
/// // ^^^ this closure executes 123 times | ||
/// // and the vecs will have the expected capacity | ||
/// ``` | ||
#[clippy::version = "1.74.0"] | ||
pub REPEAT_VEC_WITH_CAPACITY, | ||
suspicious, | ||
"repeating a `Vec::with_capacity` expression which does not retain capacity" | ||
} | ||
|
||
declare_lint_pass!(RepeatVecWithCapacity => [REPEAT_VEC_WITH_CAPACITY]); | ||
|
||
fn emit_lint(cx: &LateContext<'_>, span: Span, kind: &str, note: &'static str, sugg_msg: &'static str, sugg: String) { | ||
span_lint_and_then( | ||
cx, | ||
REPEAT_VEC_WITH_CAPACITY, | ||
span, | ||
&format!("repeating `Vec::with_capacity` using `{kind}`, which does not retain capacity"), | ||
|diag| { | ||
diag.note(note); | ||
diag.span_suggestion_verbose(span, sugg_msg, sugg, Applicability::MaybeIncorrect); | ||
}, | ||
); | ||
} | ||
|
||
/// Checks `vec![Vec::with_capacity(x); n]` | ||
fn check_vec_macro(cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if let Some(mac_call) = root_macro_call(expr.span) | ||
&& cx.tcx.is_diagnostic_item(sym::vec_macro, mac_call.def_id) | ||
&& let Some(VecArgs::Repeat(repeat_expr, len_expr)) = VecArgs::hir(cx, expr) | ||
&& fn_def_id(cx, repeat_expr).is_some_and(|did| match_def_path(cx, did, &paths::VEC_WITH_CAPACITY)) | ||
&& !len_expr.span.from_expansion() | ||
&& let Some(Constant::Int(2..)) = constant(cx, cx.typeck_results(), expr_or_init(cx, len_expr)) | ||
{ | ||
emit_lint( | ||
cx, | ||
expr.span.source_callsite(), | ||
"vec![x; n]", | ||
"only the last `Vec` will have the capacity", | ||
"if you intended to initialize multiple `Vec`s with an initial capacity, try", | ||
format!( | ||
"(0..{}).map(|_| {}).collect::<Vec<_>>()", | ||
snippet(cx, len_expr.span, ""), | ||
snippet(cx, repeat_expr.span, "..") | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// Checks `iter::repeat(Vec::with_capacity(x))` | ||
fn check_repeat_fn(cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if !expr.span.from_expansion() | ||
&& fn_def_id(cx, expr).is_some_and(|did| cx.tcx.is_diagnostic_item(sym::iter_repeat, did)) | ||
&& let ExprKind::Call(_, [repeat_expr]) = expr.kind | ||
&& fn_def_id(cx, repeat_expr).is_some_and(|did| match_def_path(cx, did, &paths::VEC_WITH_CAPACITY)) | ||
&& !repeat_expr.span.from_expansion() | ||
{ | ||
emit_lint( | ||
cx, | ||
expr.span, | ||
"iter::repeat", | ||
"none of the yielded `Vec`s will have the requested capacity", | ||
"if you intended to create an iterator that yields `Vec`s with an initial capacity, try", | ||
format!("std::iter::repeat_with(|| {})", snippet(cx, repeat_expr.span, "..")), | ||
); | ||
} | ||
} | ||
|
||
impl LateLintPass<'_> for RepeatVecWithCapacity { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
check_vec_macro(cx, expr); | ||
check_repeat_fn(cx, expr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#![warn(clippy::repeat_vec_with_capacity)] | ||
|
||
fn main() { | ||
{ | ||
(0..123).map(|_| Vec::<()>::with_capacity(42)).collect::<Vec<_>>(); | ||
//~^ ERROR: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity | ||
} | ||
|
||
{ | ||
let n = 123; | ||
(0..n).map(|_| Vec::<()>::with_capacity(42)).collect::<Vec<_>>(); | ||
//~^ ERROR: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity | ||
} | ||
|
||
{ | ||
macro_rules! from_macro { | ||
($x:expr) => { | ||
vec![$x; 123]; | ||
}; | ||
} | ||
// vec expansion is from another macro, don't lint | ||
from_macro!(Vec::<()>::with_capacity(42)); | ||
} | ||
|
||
{ | ||
std::iter::repeat_with(|| Vec::<()>::with_capacity(42)); | ||
//~^ ERROR: repeating `Vec::with_capacity` using `iter::repeat`, which does not retain capacity | ||
} | ||
|
||
{ | ||
macro_rules! from_macro { | ||
($x:expr) => { | ||
std::iter::repeat($x) | ||
}; | ||
} | ||
from_macro!(Vec::<()>::with_capacity(42)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#![warn(clippy::repeat_vec_with_capacity)] | ||
|
||
fn main() { | ||
{ | ||
vec![Vec::<()>::with_capacity(42); 123]; | ||
//~^ ERROR: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity | ||
} | ||
|
||
{ | ||
let n = 123; | ||
vec![Vec::<()>::with_capacity(42); n]; | ||
//~^ ERROR: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity | ||
} | ||
|
||
{ | ||
macro_rules! from_macro { | ||
($x:expr) => { | ||
vec![$x; 123]; | ||
}; | ||
} | ||
// vec expansion is from another macro, don't lint | ||
from_macro!(Vec::<()>::with_capacity(42)); | ||
} | ||
|
||
{ | ||
std::iter::repeat(Vec::<()>::with_capacity(42)); | ||
//~^ ERROR: repeating `Vec::with_capacity` using `iter::repeat`, which does not retain capacity | ||
} | ||
|
||
{ | ||
macro_rules! from_macro { | ||
($x:expr) => { | ||
std::iter::repeat($x) | ||
}; | ||
} | ||
from_macro!(Vec::<()>::with_capacity(42)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
error: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity | ||
--> $DIR/repeat_vec_with_capacity.rs:5:9 | ||
| | ||
LL | vec![Vec::<()>::with_capacity(42); 123]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: only the last `Vec` will have the capacity | ||
= note: `-D clippy::repeat-vec-with-capacity` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::repeat_vec_with_capacity)]` | ||
help: if you intended to initialize multiple `Vec`s with an initial capacity, try | ||
| | ||
LL | (0..123).map(|_| Vec::<()>::with_capacity(42)).collect::<Vec<_>>(); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: repeating `Vec::with_capacity` using `vec![x; n]`, which does not retain capacity | ||
--> $DIR/repeat_vec_with_capacity.rs:11:9 | ||
| | ||
LL | vec![Vec::<()>::with_capacity(42); n]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: only the last `Vec` will have the capacity | ||
help: if you intended to initialize multiple `Vec`s with an initial capacity, try | ||
| | ||
LL | (0..n).map(|_| Vec::<()>::with_capacity(42)).collect::<Vec<_>>(); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: repeating `Vec::with_capacity` using `iter::repeat`, which does not retain capacity | ||
--> $DIR/repeat_vec_with_capacity.rs:26:9 | ||
| | ||
LL | std::iter::repeat(Vec::<()>::with_capacity(42)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: none of the yielded `Vec`s will have the requested capacity | ||
help: if you intended to create an iterator that yields `Vec`s with an initial capacity, try | ||
| | ||
LL | std::iter::repeat_with(|| Vec::<()>::with_capacity(42)); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to 3 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent explanation!