Skip to content

Commit 1b85ae3

Browse files
Only emit useless_vec suggestion if the macro does not contain code comments
1 parent b57d98b commit 1b85ae3

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-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
}

0 commit comments

Comments
 (0)