From 296edda3a9fa4bdfe89bf52155432b8fcc949a8c Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 18 Nov 2017 16:10:28 +0100 Subject: [PATCH 1/3] const_static_lifetime: this applies not only to path types For example, &'static [u8] or &'static (t1, t2). --- clippy_lints/src/const_static_lifetime.rs | 29 ++++++++++++---------- tests/ui/const_static_lifetime.rs | 6 +++++ tests/ui/const_static_lifetime.stderr | 30 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/const_static_lifetime.rs b/clippy_lints/src/const_static_lifetime.rs index 6ee4dad7d..69a4c0ae8 100644 --- a/clippy_lints/src/const_static_lifetime.rs +++ b/clippy_lints/src/const_static_lifetime.rs @@ -48,20 +48,23 @@ impl StaticConst { TyKind::Rptr(ref optional_lifetime, ref borrow_type) => { // Match the 'static lifetime if let Some(lifetime) = *optional_lifetime { - if let TyKind::Path(_, _) = borrow_type.ty.node { - // Verify that the path is a str - if lifetime.ident.name == "'static" { - let mut sug: String = String::new(); - 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); - }, - ); + match borrow_type.ty.node { + TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | + TyKind::Tup(..) => { + if lifetime.ident.name == "'static" { + let mut sug: String = String::new(); + 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); + }, + ); + } } + _ => {} } } self.visit_type(&*borrow_type.ty, cx); diff --git a/tests/ui/const_static_lifetime.rs b/tests/ui/const_static_lifetime.rs index d2caf5993..a033f2b36 100644 --- a/tests/ui/const_static_lifetime.rs +++ b/tests/ui/const_static_lifetime.rs @@ -17,6 +17,12 @@ const VAR_SEVEN: &[&(&str, &'static [&'static str])] = &[&("one", &["other one"] const VAR_HEIGHT: &'static Foo = &Foo {}; +const VAR_SLICE: &'static [u8] = b"Test constant #1"; // ERROR Consider removing 'static. + +const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR Consider removing 'static. + +const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR Consider removing 'static. + fn main() { let false_positive: &'static str = "test"; println!("{}", VAR_ONE); diff --git a/tests/ui/const_static_lifetime.stderr b/tests/ui/const_static_lifetime.stderr index 1eeb27c24..d4558f7b2 100644 --- a/tests/ui/const_static_lifetime.stderr +++ b/tests/ui/const_static_lifetime.stderr @@ -24,6 +24,12 @@ error: Constants have by default a `'static` lifetime 10 | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR Consider removing 'static | ^^^^^^^ help: consider removing `'static` +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` + error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:12:30 | @@ -36,6 +42,12 @@ error: Constants have by default a `'static` lifetime 14 | const VAR_SIX: &'static u8 = &5; | ^^^^^^^ help: consider removing `'static` +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` + error: Constants have by default a `'static` lifetime --> $DIR/const_static_lifetime.rs:16:39 | @@ -48,3 +60,21 @@ error: Constants have by default a `'static` lifetime 18 | const VAR_HEIGHT: &'static Foo = &Foo {}; | ^^^^^^^ help: consider removing `'static` +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` + +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` + +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` + From 3d26c7bb7f4fe9a10c17db7dc94c46b741e45fa8 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 18 Nov 2017 16:11:07 +0100 Subject: [PATCH 2/3] CONTRIBUTING: clarify how to regenerate ui test output --- CONTRIBUTING.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 81618aace..c5dcec216 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -70,8 +70,9 @@ Please document your lint with a doc comment akin to the following: Clippy uses UI tests. UI tests check that the output of the compiler is exactly as expected. Of course there's little sense in writing the output yourself or copying it around. -Therefore you can simply run `tests/ui/update-all-references.sh` and check whether -the output looks as you expect with `git diff`. Commit all `*.stderr` files, too. +Therefore you can simply run `tests/ui/update-all-references.sh` (after running +`cargo test`) and check whether the output looks as you expect with `git diff`. Commit all +`*.stderr` files, too. ### Testing manually From 76324851b58652d05c92c084f3274748261ca1a4 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 18 Nov 2017 16:11:55 +0100 Subject: [PATCH 3/3] tests: fixup arg handling for update-all-references This script does not take any args, so $1 being empty is expected. --- tests/ui/update-all-references.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ui/update-all-references.sh b/tests/ui/update-all-references.sh index d6aa69c7e..acc38f15f 100755 --- a/tests/ui/update-all-references.sh +++ b/tests/ui/update-all-references.sh @@ -18,7 +18,7 @@ # # See all `update-references.sh`, if you just want to update a single test. -if [[ "$1" == "--help" || "$1" == "-h" || "$1" == "" ]]; then +if [[ "$1" == "--help" || "$1" == "-h" ]]; then echo "usage: $0" fi