mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-17 22:48:48 +00:00
inline construct_lint_suggestions
This commit is contained in:
parent
dc23b5d661
commit
ed3744b957
1 changed files with 19 additions and 42 deletions
|
@ -56,39 +56,6 @@ impl LateLintPass<'_> for RcCloneInVecInit {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct LintSuggestion {
|
|
||||||
message: String,
|
|
||||||
snippet: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn construct_lint_suggestions(
|
|
||||||
cx: &LateContext<'_>,
|
|
||||||
span: Span,
|
|
||||||
symbol_name: &str,
|
|
||||||
elem: &Expr<'_>,
|
|
||||||
len: &Expr<'_>,
|
|
||||||
) -> Vec<LintSuggestion> {
|
|
||||||
let len_snippet = snippet(cx, len.span, "..");
|
|
||||||
let elem_snippet = elem_snippet(cx, elem, symbol_name);
|
|
||||||
let indentation = indent_of(cx, span).unwrap_or(0);
|
|
||||||
let indentation = " ".repeat(indentation);
|
|
||||||
let loop_init_suggestion = loop_init_suggestion(&elem_snippet, len_snippet.as_ref(), &indentation);
|
|
||||||
let extract_suggestion = extract_suggestion(&elem_snippet, len_snippet.as_ref(), &indentation);
|
|
||||||
|
|
||||||
vec![
|
|
||||||
LintSuggestion {
|
|
||||||
message: format!("consider initializing each `{symbol_name}` element individually"),
|
|
||||||
snippet: loop_init_suggestion,
|
|
||||||
},
|
|
||||||
LintSuggestion {
|
|
||||||
message: format!(
|
|
||||||
"or if this is intentional, consider extracting the `{symbol_name}` initialization to a variable"
|
|
||||||
),
|
|
||||||
snippet: extract_suggestion,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn elem_snippet(cx: &LateContext<'_>, elem: &Expr<'_>, symbol_name: &str) -> String {
|
fn elem_snippet(cx: &LateContext<'_>, elem: &Expr<'_>, symbol_name: &str) -> String {
|
||||||
let elem_snippet = snippet(cx, elem.span, "..").to_string();
|
let elem_snippet = snippet(cx, elem.span, "..").to_string();
|
||||||
if elem_snippet.contains('\n') {
|
if elem_snippet.contains('\n') {
|
||||||
|
@ -131,17 +98,27 @@ fn emit_lint(cx: &LateContext<'_>, symbol: Symbol, lint_span: Span, elem: &Expr<
|
||||||
lint_span,
|
lint_span,
|
||||||
&format!("calling `{symbol_name}::new` in `vec![elem; len]`"),
|
&format!("calling `{symbol_name}::new` in `vec![elem; len]`"),
|
||||||
|diag| {
|
|diag| {
|
||||||
let suggestions = construct_lint_suggestions(cx, lint_span, symbol_name, elem, len);
|
let len_snippet = snippet(cx, len.span, "..");
|
||||||
|
let elem_snippet = elem_snippet(cx, elem, symbol_name);
|
||||||
|
let indentation = " ".repeat(indent_of(cx, lint_span).unwrap_or(0));
|
||||||
|
let loop_init_suggestion = loop_init_suggestion(&elem_snippet, len_snippet.as_ref(), &indentation);
|
||||||
|
let extract_suggestion = extract_suggestion(&elem_snippet, len_snippet.as_ref(), &indentation);
|
||||||
|
|
||||||
diag.note(format!("each element will point to the same `{symbol_name}` instance"));
|
diag.note(format!("each element will point to the same `{symbol_name}` instance"));
|
||||||
for suggestion in suggestions {
|
diag.span_suggestion(
|
||||||
diag.span_suggestion(
|
lint_span,
|
||||||
lint_span,
|
format!("consider initializing each `{symbol_name}` element individually"),
|
||||||
&suggestion.message,
|
loop_init_suggestion,
|
||||||
&suggestion.snippet,
|
Applicability::Unspecified,
|
||||||
Applicability::Unspecified,
|
);
|
||||||
);
|
diag.span_suggestion(
|
||||||
}
|
lint_span,
|
||||||
|
format!(
|
||||||
|
"or if this is intentional, consider extracting the `{symbol_name}` initialization to a variable"
|
||||||
|
),
|
||||||
|
extract_suggestion,
|
||||||
|
Applicability::Unspecified,
|
||||||
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue