-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Implementation of the const_static_lifetime
lint.
#2151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
322effe
Implementation of the `const_static_lifetime` lint.
gbip 4069313
Fixed some code in clippy to pass the new, removed formatting changes.
gbip fbce504
Added the test results.
gbip 4bbda68
Better linting : use of span_lint_and_then.
gbip 0928168
Remove "#![feature(plugin)]" in the test".
gbip acdd93a
Final .stderr for `const_static_lifetime`.
gbip 625aae7
Merge branch 'master' of https://github.com/rust-lang-nursery/rust-cl…
gbip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use syntax::ast::{Item, ItemKind, TyKind, Ty}; | ||
use rustc::lint::{LintPass, EarlyLintPass, LintArray, EarlyContext}; | ||
use utils::{span_lint_and_then, in_macro}; | ||
|
||
/// **What it does:** Checks for constants with an explicit `'static` lifetime. | ||
/// | ||
/// **Why is this bad?** Adding `'static` to every reference can create very | ||
/// complicated types. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] = | ||
/// &[...] | ||
/// ``` | ||
/// This code can be rewritten as | ||
/// ```rust | ||
/// const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...] | ||
/// ``` | ||
|
||
declare_lint! { | ||
pub CONST_STATIC_LIFETIME, | ||
Warn, | ||
"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 | ||
fn visit_type(&mut self, ty: &Ty, cx: &EarlyContext) { | ||
match ty.node { | ||
// Be carefull of nested structures (arrays and tuples) | ||
TyKind::Array(ref ty, _) => { | ||
self.visit_type(&*ty, cx); | ||
}, | ||
TyKind::Tup(ref tup) => { | ||
for tup_ty in tup { | ||
self.visit_type(&*tup_ty, cx); | ||
} | ||
}, | ||
// 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 { | ||
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);}); | ||
} | ||
} | ||
} | ||
self.visit_type(&*borrow_type.ty, cx); | ||
}, | ||
TyKind::Slice(ref ty) => { | ||
self.visit_type(ty, cx); | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
} | ||
|
||
impl EarlyLintPass for StaticConst { | ||
fn check_item(&mut self, cx: &EarlyContext, item: &Item) { | ||
if !in_macro(item.span) { | ||
// Match only constants... | ||
if let ItemKind::Const(ref var_type, _) = item.node { | ||
self.visit_type(var_type, cx); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't make unrelated formatting changes