@@ -8,7 +8,7 @@ use clippy_utils::msrvs::{self, Msrv};
88use clippy_utils:: source:: SpanRangeExt ;
99use clippy_utils:: ty:: is_copy;
1010use 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 } ;
1212use rustc_errors:: Applicability ;
1313use rustc_hir:: { BorrowKind , Expr , ExprKind , HirId , LetStmt , Mutability , Node , Pat , PatKind } ;
1414use 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