@@ -56,39 +56,6 @@ impl LateLintPass<'_> for RcCloneInVecInit {
5656 }
5757}
5858
59- struct LintSuggestion {
60- message : String ,
61- snippet : String ,
62- }
63-
64- fn construct_lint_suggestions (
65- cx : & LateContext < ' _ > ,
66- span : Span ,
67- symbol_name : & str ,
68- elem : & Expr < ' _ > ,
69- len : & Expr < ' _ > ,
70- ) -> Vec < LintSuggestion > {
71- let len_snippet = snippet ( cx, len. span , ".." ) ;
72- let elem_snippet = elem_snippet ( cx, elem, symbol_name) ;
73- let indentation = indent_of ( cx, span) . unwrap_or ( 0 ) ;
74- let indentation = " " . repeat ( indentation) ;
75- let loop_init_suggestion = loop_init_suggestion ( & elem_snippet, len_snippet. as_ref ( ) , & indentation) ;
76- let extract_suggestion = extract_suggestion ( & elem_snippet, len_snippet. as_ref ( ) , & indentation) ;
77-
78- vec ! [
79- LintSuggestion {
80- message: format!( "consider initializing each `{symbol_name}` element individually" ) ,
81- snippet: loop_init_suggestion,
82- } ,
83- LintSuggestion {
84- message: format!(
85- "or if this is intentional, consider extracting the `{symbol_name}` initialization to a variable"
86- ) ,
87- snippet: extract_suggestion,
88- } ,
89- ]
90- }
91-
9259fn elem_snippet ( cx : & LateContext < ' _ > , elem : & Expr < ' _ > , symbol_name : & str ) -> String {
9360 let elem_snippet = snippet ( cx, elem. span , ".." ) . to_string ( ) ;
9461 if elem_snippet. contains ( '\n' ) {
@@ -131,17 +98,27 @@ fn emit_lint(cx: &LateContext<'_>, symbol: Symbol, lint_span: Span, elem: &Expr<
13198 lint_span,
13299 & format ! ( "calling `{symbol_name}::new` in `vec![elem; len]`" ) ,
133100 |diag| {
134- let suggestions = construct_lint_suggestions ( cx, lint_span, symbol_name, elem, len) ;
101+ let len_snippet = snippet ( cx, len. span , ".." ) ;
102+ let elem_snippet = elem_snippet ( cx, elem, symbol_name) ;
103+ let indentation = " " . repeat ( indent_of ( cx, lint_span) . unwrap_or ( 0 ) ) ;
104+ let loop_init_suggestion = loop_init_suggestion ( & elem_snippet, len_snippet. as_ref ( ) , & indentation) ;
105+ let extract_suggestion = extract_suggestion ( & elem_snippet, len_snippet. as_ref ( ) , & indentation) ;
135106
136107 diag. note ( format ! ( "each element will point to the same `{symbol_name}` instance" ) ) ;
137- for suggestion in suggestions {
138- diag. span_suggestion (
139- lint_span,
140- & suggestion. message ,
141- & suggestion. snippet ,
142- Applicability :: Unspecified ,
143- ) ;
144- }
108+ diag. span_suggestion (
109+ lint_span,
110+ format ! ( "consider initializing each `{symbol_name}` element individually" ) ,
111+ loop_init_suggestion,
112+ Applicability :: Unspecified ,
113+ ) ;
114+ diag. span_suggestion (
115+ lint_span,
116+ format ! (
117+ "or if this is intentional, consider extracting the `{symbol_name}` initialization to a variable"
118+ ) ,
119+ extract_suggestion,
120+ Applicability :: Unspecified ,
121+ ) ;
145122 } ,
146123 ) ;
147124}
0 commit comments