From 37f62a54f83b094cb5de55345945d1fb58d7d1b0 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Tue, 16 Jan 2018 20:46:43 +0530 Subject: [PATCH] Show wider and more accurate suggestion for const_static_lifetime fixes #2365 --- clippy_lints/src/const_static_lifetime.rs | 7 +++--- tests/ui/const_static_lifetime.stderr | 26 +++++++++++------------ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/clippy_lints/src/const_static_lifetime.rs b/clippy_lints/src/const_static_lifetime.rs index 3ad3be181d2..293d63daaaa 100644 --- a/clippy_lints/src/const_static_lifetime.rs +++ b/clippy_lints/src/const_static_lifetime.rs @@ -1,6 +1,6 @@ use syntax::ast::{Item, ItemKind, Ty, TyKind}; use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; -use utils::{in_macro, span_lint_and_then}; +use utils::{in_macro, snippet, span_lint_and_then}; /// **What it does:** Checks for constants with an explicit `'static` lifetime. /// @@ -51,14 +51,15 @@ impl StaticConst { TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => { if lifetime.ident.name == "'static" { - let mut sug: String = String::new(); + let snip = snippet(cx, borrow_type.ty.span, ""); + let sugg = format!("&{}", snip); span_lint_and_then( cx, CONST_STATIC_LIFETIME, lifetime.span, "Constants have by default a `'static` lifetime", |db| { - db.span_suggestion(lifetime.span, "consider removing `'static`", sug); + db.span_suggestion(ty.span, "consider removing `'static`", sugg); }, ); } diff --git a/tests/ui/const_static_lifetime.stderr b/tests/ui/const_static_lifetime.stderr index d4558f7b241..448d83ee921 100644 --- a/tests/ui/const_static_lifetime.stderr +++ b/tests/ui/const_static_lifetime.stderr @@ -2,7 +2,7 @@ error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:4:17 | 4 | const VAR_ONE: &'static str = "Test constant #1"; // ERROR Consider removing 'static. - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^---- help: consider removing `'static`: `&str` | = note: `-D const-static-lifetime` implied by `-D warnings` @@ -10,71 +10,71 @@ error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:8:21 | 8 | const VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR Consider removing 'static - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:10:32 | 10 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:10:47 | 10 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:12:18 | 12 | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^------------------ help: consider removing `'static`: `&[&[&'static str]]` error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:12:30 | 12 | const VAR_FIVE: &'static [&[&'static str]] = &[&["test"], &["other one"]]; // ERROR Consider removing 'static - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:14:17 | 14 | const VAR_SIX: &'static u8 = &5; - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^--- help: consider removing `'static`: `&u8` error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:16:29 | 16 | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^--------------- help: consider removing `'static`: `&[&'static str]` error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:16:39 | 16 | const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"])]; - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^---- help: consider removing `'static`: `&str` error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:18:20 | 18 | const VAR_HEIGHT: &'static Foo = &Foo {}; - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^---- help: consider removing `'static`: `&Foo` error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:20:19 | 20 | const VAR_SLICE: &'static [u8] = b"Test constant #1"; // ERROR Consider removing 'static. - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^----- help: consider removing `'static`: `&[u8]` error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:22:19 | 22 | const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:24:19 | 24 | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. - | ^^^^^^^ help: consider removing `'static` + | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]`