Skip to content

Commit ad69c65

Browse files
authored
Only emit useless_vec suggestion if the macro does not contain code comments (rust-lang#13911)
Fixes rust-lang#13692. If the `vec!` macro call contains comments, we should not provide suggestions and let users handle it however they see fit. changelog: Only emit `useless_vec` suggestion if the macro does not contain code comments
2 parents 54f88c3 + b76e042 commit ad69c65

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

clippy_lints/src/vec.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use clippy_utils::msrvs::{self, Msrv};
88
use clippy_utils::source::SpanRangeExt;
99
use clippy_utils::ty::is_copy;
1010
use clippy_utils::visitors::for_each_local_use_after_expr;
11-
use clippy_utils::{get_parent_expr, higher, is_in_test, is_trait_method};
11+
use clippy_utils::{get_parent_expr, higher, is_in_test, is_trait_method, span_contains_comment};
1212
use rustc_errors::Applicability;
1313
use rustc_hir::{BorrowKind, Expr, ExprKind, HirId, LetStmt, Mutability, Node, Pat, PatKind};
1414
use rustc_lint::{LateContext, LateLintPass};
@@ -132,9 +132,19 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
132132
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
133133
for (span, lint_opt) in &self.span_to_lint_map {
134134
if let Some((hir_id, suggest_slice, snippet, applicability)) = lint_opt {
135-
let help_msg = format!("you can use {} directly", suggest_slice.desc(),);
135+
let help_msg = format!("you can use {} directly", suggest_slice.desc());
136136
span_lint_hir_and_then(cx, USELESS_VEC, *hir_id, *span, "useless use of `vec!`", |diag| {
137-
diag.span_suggestion(*span, help_msg, snippet, *applicability);
137+
// If the `vec!` macro contains comment, better not make the suggestion machine
138+
// applicable as it would remove them.
139+
let applicability = if *applicability != Applicability::Unspecified
140+
&& let source_map = cx.tcx.sess.source_map()
141+
&& span_contains_comment(source_map, *span)
142+
{
143+
Applicability::Unspecified
144+
} else {
145+
*applicability
146+
};
147+
diag.span_suggestion(*span, help_msg, snippet, applicability);
138148
});
139149
}
140150
}

tests/ui/useless_vec.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@no-rustfix: no suggestions
2+
3+
#![warn(clippy::useless_vec)]
4+
5+
// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13692>.
6+
fn foo() {
7+
// There should be no suggestion in this case.
8+
let _some_variable = vec![
9+
//~^ useless_vec
10+
1, 2, // i'm here to stay
11+
3, 4, // but this one going away ;-;
12+
]; // that is life anyways
13+
}
14+
15+
fn main() {}

tests/ui/useless_vec.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: useless use of `vec!`
2+
--> tests/ui/useless_vec.rs:8:26
3+
|
4+
LL | let _some_variable = vec![
5+
| __________________________^
6+
LL | |
7+
LL | | 1, 2, // i'm here to stay
8+
LL | | 3, 4, // but this one going away ;-;
9+
LL | | ]; // that is life anyways
10+
| |_____^
11+
|
12+
= note: `-D clippy::useless-vec` implied by `-D warnings`
13+
= help: to override `-D warnings` add `#[allow(clippy::useless_vec)]`
14+
help: you can use an array directly
15+
|
16+
LL ~ let _some_variable = [1, 2, // i'm here to stay
17+
LL ~ 3, 4]; // that is life anyways
18+
|
19+
20+
error: aborting due to 1 previous error
21+

0 commit comments

Comments
 (0)