Skip to content

Commit 28bd591

Browse files
committed
Only build suggestion if necessary in USELESS_VEC
1 parent 98f18f0 commit 28bd591

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

clippy_lints/src/vec.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,26 @@ impl LateLintPass for Pass {
5151

5252
fn check_vec_macro(cx: &LateContext, vec: &Expr, span: Span) {
5353
if let Some(vec_args) = higher::vec_macro(cx, vec) {
54-
let snippet = match vec_args {
55-
higher::VecArgs::Repeat(elem, len) => {
56-
format!("&[{}; {}]", snippet(cx, elem.span, "elem"), snippet(cx, len.span, "len")).into()
57-
}
58-
higher::VecArgs::Vec(args) => {
59-
if let Some(last) = args.iter().last() {
60-
let span = Span {
61-
lo: args[0].span.lo,
62-
hi: last.span.hi,
63-
expn_id: args[0].span.expn_id,
64-
};
54+
span_lint_and_then(cx, USELESS_VEC, span, "useless use of `vec!`", |db| {
55+
let snippet = match vec_args {
56+
higher::VecArgs::Repeat(elem, len) => {
57+
format!("&[{}; {}]", snippet(cx, elem.span, "elem"), snippet(cx, len.span, "len")).into()
58+
}
59+
higher::VecArgs::Vec(args) => {
60+
if let Some(last) = args.iter().last() {
61+
let span = Span {
62+
lo: args[0].span.lo,
63+
hi: last.span.hi,
64+
expn_id: args[0].span.expn_id,
65+
};
6566

66-
format!("&[{}]", snippet(cx, span, "..")).into()
67-
} else {
68-
"&[]".into()
67+
format!("&[{}]", snippet(cx, span, "..")).into()
68+
} else {
69+
"&[]".into()
70+
}
6971
}
70-
}
71-
};
72+
};
7273

73-
span_lint_and_then(cx, USELESS_VEC, span, "useless use of `vec!`", |db| {
7474
db.span_suggestion(span, "you can use a slice directly", snippet);
7575
});
7676
}

0 commit comments

Comments
 (0)