2018-11-27 20:14:15 +00:00
|
|
|
use crate::utils::{in_macro, snippet, span_lint_and_then};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
|
|
|
|
use rustc::{declare_tool_lint, lint_array};
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use syntax::ast::*;
|
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)
|
|
|
|
}
|
2019-01-26 19:40:55 +00:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"StaticConst"
|
|
|
|
}
|
2017-10-20 13:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|