1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Some lints that are built in to the compiler.
13 //! These are the built-in lints that are emitted direct in the main
14 //! compiler code, rather than using their own custom pass. Those
15 //! lints are all available in `rustc_lint::builtin`.
17 use errors::{Applicability, DiagnosticBuilder};
18 use lint::{LintPass, LateLintPass, LintArray};
20 use syntax::codemap::Span;
23 pub EXCEEDING_BITSHIFTS,
25 "shift exceeds the type's number of bits"
31 "constant evaluation detected erroneous expression"
37 "imports that are never used"
41 pub UNUSED_EXTERN_CRATES,
43 "extern crates that are never used"
47 pub UNUSED_QUALIFICATIONS,
49 "detects unnecessarily qualified names"
55 "unrecognized lint attribute"
61 "detect variables which are not used in any way"
65 pub UNUSED_ASSIGNMENTS,
67 "detect assignments that will never be read"
73 "detect unused, unexported items"
79 "detects unreachable code paths"
83 pub UNREACHABLE_PATTERNS,
85 "detects unreachable patterns"
91 "detects macros that were not used"
97 "mass-change the level for lints which produce warnings"
103 "unused or unknown features found in crate-level #[feature] directives"
109 "stable features found in #[feature] directive"
113 pub UNKNOWN_CRATE_TYPES,
115 "unknown crate type found in #[crate_type] directive"
121 "detects trivial casts which could be removed"
125 pub TRIVIAL_NUMERIC_CASTS,
127 "detects trivial casts of numeric types which could be removed"
131 pub PRIVATE_IN_PUBLIC,
133 "detect private items in public interfaces not caught by the old implementation"
137 pub PUB_USE_OF_PRIVATE_EXTERN_CRATE,
139 "detect public re-exports of private extern crates"
143 pub INVALID_TYPE_PARAM_DEFAULT,
145 "type parameter default erroneously allowed in invalid location"
149 pub RENAMED_AND_REMOVED_LINTS,
151 "lints that have been renamed or removed"
155 pub SAFE_EXTERN_STATICS,
157 "safe access to extern statics was erroneously allowed"
161 pub SAFE_PACKED_BORROWS,
163 "safe borrows of fields of packed structs were was erroneously allowed"
167 pub PATTERNS_IN_FNS_WITHOUT_BODY,
169 "patterns in functions without body were erroneously allowed"
173 pub LEGACY_DIRECTORY_OWNERSHIP,
175 "non-inline, non-`#[path]` modules (e.g. `mod foo;`) were erroneously allowed in some files \
182 "detects names that resolve to ambiguous glob imports with RFC 1560"
186 pub LEGACY_CONSTRUCTOR_VISIBILITY,
188 "detects use of struct constructors that would be invisible with new visibility rules"
192 pub MISSING_FRAGMENT_SPECIFIER,
194 "detects missing fragment specifiers in unused `macro_rules!` patterns"
198 pub PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
200 "detects parenthesized generic parameters in type and module names"
204 pub LATE_BOUND_LIFETIME_ARGUMENTS,
206 "detects generic lifetime arguments in path segments with late bound lifetime parameters"
210 pub INCOHERENT_FUNDAMENTAL_IMPLS,
212 "potentially-conflicting impls were erroneously allowed"
218 "detects use of deprecated items"
224 "unnecessary use of an `unsafe` block"
230 "detect mut variables which don't need to be mutable"
234 pub SINGLE_USE_LIFETIME,
236 "detects single use lifetimes"
240 pub TYVAR_BEHIND_RAW_POINTER,
242 "raw pointer to an inference variable"
246 pub ELIDED_LIFETIME_IN_PATH,
248 "hidden lifetime parameters are deprecated, try `Foo<'_>`"
252 pub BARE_TRAIT_OBJECT,
254 "suggest using `dyn Trait` for trait objects"
258 pub ABSOLUTE_PATH_STARTING_WITH_MODULE,
260 "fully qualified paths that start with a module name \
261 instead of `crate`, `self`, or an extern crate name"
265 pub ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
267 "floating-point literals cannot be used in patterns"
271 pub UNSTABLE_NAME_COLLISION,
273 "detects name collision with an existing but unstable method"
276 /// Does nothing as a lint pass, but registers some `Lint`s
277 /// which are used by other parts of the compiler.
278 #[derive(Copy, Clone)]
279 pub struct HardwiredLints;
281 impl LintPass for HardwiredLints {
282 fn get_lints(&self) -> LintArray {
284 ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
287 UNUSED_EXTERN_CRATES,
288 UNUSED_QUALIFICATIONS,
294 UNREACHABLE_PATTERNS,
301 TRIVIAL_NUMERIC_CASTS,
303 PUB_USE_OF_PRIVATE_EXTERN_CRATE,
304 INVALID_TYPE_PARAM_DEFAULT,
306 RENAMED_AND_REMOVED_LINTS,
309 PATTERNS_IN_FNS_WITHOUT_BODY,
310 LEGACY_DIRECTORY_OWNERSHIP,
312 LEGACY_CONSTRUCTOR_VISIBILITY,
313 MISSING_FRAGMENT_SPECIFIER,
314 PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
315 LATE_BOUND_LIFETIME_ARGUMENTS,
316 INCOHERENT_FUNDAMENTAL_IMPLS,
321 TYVAR_BEHIND_RAW_POINTER,
322 ELIDED_LIFETIME_IN_PATH,
324 ABSOLUTE_PATH_STARTING_WITH_MODULE,
325 UNSTABLE_NAME_COLLISION,
330 // this could be a closure, but then implementing derive traits
331 // becomes hacky (and it gets allocated)
332 #[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
333 pub enum BuiltinLintDiagnostics {
335 BareTraitObject(Span, /* is_global */ bool),
336 AbsPathWithModule(Span),
339 impl BuiltinLintDiagnostics {
340 pub fn run(self, sess: &Session, db: &mut DiagnosticBuilder) {
342 BuiltinLintDiagnostics::Normal => (),
343 BuiltinLintDiagnostics::BareTraitObject(span, is_global) => {
344 let (sugg, app) = match sess.codemap().span_to_snippet(span) {
345 Ok(ref s) if is_global => (format!("dyn ({})", s),
346 Applicability::MachineApplicable),
347 Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
348 Err(_) => (format!("dyn <type>"), Applicability::HasPlaceholders)
350 db.span_suggestion_with_applicability(span, "use `dyn`", sugg, app);
352 BuiltinLintDiagnostics::AbsPathWithModule(span) => {
353 let (sugg, app) = match sess.codemap().span_to_snippet(span) {
355 // FIXME(Manishearth) ideally the emitting code
356 // can tell us whether or not this is global
357 let opt_colon = if s.trim_left().starts_with("::") {
363 (format!("crate{}{}", opt_colon, s), Applicability::MachineApplicable)
365 Err(_) => (format!("crate::<path>"), Applicability::HasPlaceholders)
367 db.span_suggestion_with_applicability(span, "use `crate`", sugg, app);
373 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HardwiredLints {}