2018-10-06 16:18:06 +00:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-09-15 07:21:58 +00:00
|
|
|
use crate::rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
|
|
|
|
use crate::rustc::{declare_tool_lint, lint_array};
|
2018-09-15 16:14:08 +00:00
|
|
|
use crate::rustc_errors::Applicability;
|
2018-11-27 20:14:15 +00:00
|
|
|
use crate::syntax::ast::*;
|
|
|
|
use crate::utils::{in_macro, snippet, span_lint_and_then};
|
2017-10-20 13:51:35 +00:00
|
|
|
|
|
|
|
/// **What it does:** Checks for constants with an explicit `'static` lifetime.
|
|
|
|
///
|
2017-10-20 14:00:44 +00:00
|
|
|
/// **Why is this bad?** Adding `'static` to every reference can create very
|
|
|
|
/// complicated types.
|
2017-10-20 13:51:35 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2017-10-20 14:00:44 +00:00
|
|
|
/// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
|
|
|
|
/// &[...]
|
2017-10-20 13:51:35 +00:00
|
|
|
/// ```
|
|
|
|
/// This code can be rewritten as
|
|
|
|
/// ```rust
|
|
|
|
/// const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
|
|
|
|
/// ```
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2017-11-04 19:55:56 +00:00
|
|
|
pub CONST_STATIC_LIFETIME,
|
2018-03-28 13:24:26 +00:00
|
|
|
style,
|
2017-10-20 13:51:35 +00:00
|
|
|
"Using explicit `'static` lifetime for constants when elision rules would allow omitting them."
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StaticConst;
|
|
|
|
|
|
|
|
impl LintPass for StaticConst {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(CONST_STATIC_LIFETIME)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StaticConst {
|
|
|
|
// Recursively visit types
|
2018-07-23 11:01:12 +00:00
|
|
|
fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) {
|
2017-10-20 13:51:35 +00:00
|
|
|
match ty.node {
|
2017-10-31 07:34:27 +00:00
|
|
|
// Be careful of nested structures (arrays and tuples)
|
2017-10-20 13:51:35 +00:00
|
|
|
TyKind::Array(ref ty, _) => {
|
|
|
|
self.visit_type(&*ty, cx);
|
|
|
|
},
|
2018-11-27 20:14:15 +00:00
|
|
|
TyKind::Tup(ref tup) => {
|
|
|
|
for tup_ty in tup {
|
|
|
|
self.visit_type(&*tup_ty, cx);
|
|
|
|
}
|
2017-10-20 13:51:35 +00:00
|
|
|
},
|
|
|
|
// This is what we are looking for !
|
|
|
|
TyKind::Rptr(ref optional_lifetime, ref borrow_type) => {
|
|
|
|
// Match the 'static lifetime
|
|
|
|
if let Some(lifetime) = *optional_lifetime {
|
2017-11-18 15:10:28 +00:00
|
|
|
match borrow_type.ty.node {
|
2018-11-27 20:14:15 +00:00
|
|
|
TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => {
|
2017-11-18 15:10:28 +00:00
|
|
|
if lifetime.ident.name == "'static" {
|
2018-01-16 15:16:43 +00:00
|
|
|
let snip = snippet(cx, borrow_type.ty.span, "<type>");
|
|
|
|
let sugg = format!("&{}", snip);
|
2017-11-18 15:10:28 +00:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
CONST_STATIC_LIFETIME,
|
2018-04-07 05:22:23 +00:00
|
|
|
lifetime.ident.span,
|
2017-11-18 15:10:28 +00:00
|
|
|
"Constants have by default a `'static` lifetime",
|
|
|
|
|db| {
|
2018-09-15 16:14:08 +00:00
|
|
|
db.span_suggestion_with_applicability(
|
2018-11-27 20:14:15 +00:00
|
|
|
ty.span,
|
2018-09-15 16:14:08 +00:00
|
|
|
"consider removing `'static`",
|
|
|
|
sugg,
|
2018-09-18 17:01:17 +00:00
|
|
|
Applicability::MachineApplicable, //snippet
|
2018-09-18 15:07:54 +00:00
|
|
|
);
|
2017-11-18 15:10:28 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2018-11-27 20:14:15 +00:00
|
|
|
},
|
|
|
|
_ => {},
|
2017-10-20 13:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.visit_type(&*borrow_type.ty, cx);
|
|
|
|
},
|
|
|
|
TyKind::Slice(ref ty) => {
|
2017-10-20 12:41:24 +00:00
|
|
|
self.visit_type(ty, cx);
|
2017-10-20 13:51:35 +00:00
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for StaticConst {
|
2018-07-23 11:01:12 +00:00
|
|
|
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
|
2017-10-20 13:51:35 +00:00
|
|
|
if !in_macro(item.span) {
|
|
|
|
// Match only constants...
|
|
|
|
if let ItemKind::Const(ref var_type, _) = item.node {
|
|
|
|
self.visit_type(var_type, cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 07:03:21 +00:00
|
|
|
|
2018-02-06 18:22:34 +00:00
|
|
|
// Don't check associated consts because `'static` cannot be elided on those (issue #2438)
|
2017-10-20 13:51:35 +00:00
|
|
|
}
|