2018-11-27 14:14:15 -06:00
|
|
|
use crate::utils::{in_macro, snippet, span_lint_and_then};
|
2018-12-29 09:04:45 -06: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 08:51:35 -05:00
|
|
|
|
|
|
|
/// **What it does:** Checks for constants with an explicit `'static` lifetime.
|
|
|
|
///
|
2017-10-20 09:00:44 -05:00
|
|
|
/// **Why is this bad?** Adding `'static` to every reference can create very
|
|
|
|
/// complicated types.
|
2017-10-20 08:51:35 -05:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2017-10-20 09:00:44 -05:00
|
|
|
/// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
|
|
|
|
/// &[...]
|
2017-10-20 08:51:35 -05:00
|
|
|
/// ```
|
|
|
|
/// This code can be rewritten as
|
|
|
|
/// ```rust
|
|
|
|
/// const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
|
|
|
|
/// ```
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2017-11-04 14:55:56 -05:00
|
|
|
pub CONST_STATIC_LIFETIME,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2017-10-20 08:51:35 -05: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 13:40:55 -06:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"StaticConst"
|
|
|
|
}
|
2017-10-20 08:51:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StaticConst {
|
|
|
|
// Recursively visit types
|
2018-07-23 06:01:12 -05:00
|
|
|
fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext<'_>) {
|
2017-10-20 08:51:35 -05:00
|
|
|
match ty.node {
|
2017-10-31 02:34:27 -05:00
|
|
|
// Be careful of nested structures (arrays and tuples)
|
2017-10-20 08:51:35 -05:00
|
|
|
TyKind::Array(ref ty, _) => {
|
|
|
|
self.visit_type(&*ty, cx);
|
|
|
|
},
|
2018-11-27 14:14:15 -06:00
|
|
|
TyKind::Tup(ref tup) => {
|
|
|
|
for tup_ty in tup {
|
|
|
|
self.visit_type(&*tup_ty, cx);
|
|
|
|
}
|
2017-10-20 08:51:35 -05: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 09:10:28 -06:00
|
|
|
match borrow_type.ty.node {
|
2018-11-27 14:14:15 -06:00
|
|
|
TyKind::Path(..) | TyKind::Slice(..) | TyKind::Array(..) | TyKind::Tup(..) => {
|
2017-11-18 09:10:28 -06:00
|
|
|
if lifetime.ident.name == "'static" {
|
2018-01-16 09:16:43 -06:00
|
|
|
let snip = snippet(cx, borrow_type.ty.span, "<type>");
|
|
|
|
let sugg = format!("&{}", snip);
|
2017-11-18 09:10:28 -06:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
CONST_STATIC_LIFETIME,
|
2018-04-07 00:22:23 -05:00
|
|
|
lifetime.ident.span,
|
2017-11-18 09:10:28 -06:00
|
|
|
"Constants have by default a `'static` lifetime",
|
|
|
|
|db| {
|
2019-01-27 06:33:56 -06:00
|
|
|
db.span_suggestion(
|
2018-11-27 14:14:15 -06:00
|
|
|
ty.span,
|
2018-09-15 11:14:08 -05:00
|
|
|
"consider removing `'static`",
|
|
|
|
sugg,
|
2018-09-18 12:01:17 -05:00
|
|
|
Applicability::MachineApplicable, //snippet
|
2018-09-18 10:07:54 -05:00
|
|
|
);
|
2017-11-18 09:10:28 -06:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2018-11-27 14:14:15 -06:00
|
|
|
},
|
|
|
|
_ => {},
|
2017-10-20 08:51:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.visit_type(&*borrow_type.ty, cx);
|
|
|
|
},
|
|
|
|
TyKind::Slice(ref ty) => {
|
2017-10-20 07:41:24 -05:00
|
|
|
self.visit_type(ty, cx);
|
2017-10-20 08:51:35 -05:00
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for StaticConst {
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
|
2017-10-20 08:51:35 -05: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 01:03:21 -06:00
|
|
|
|
2018-02-06 12:22:34 -06:00
|
|
|
// Don't check associated consts because `'static` cannot be elided on those (issue #2438)
|
2017-10-20 08:51:35 -05:00
|
|
|
}
|