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 \
180 pub LEGACY_CONSTRUCTOR_VISIBILITY,
182 "detects use of struct constructors that would be invisible with new visibility rules"
186 pub MISSING_FRAGMENT_SPECIFIER,
188 "detects missing fragment specifiers in unused `macro_rules!` patterns"
192 pub PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
194 "detects parenthesized generic parameters in type and module names"
198 pub LATE_BOUND_LIFETIME_ARGUMENTS,
200 "detects generic lifetime arguments in path segments with late bound lifetime parameters"
204 pub INCOHERENT_FUNDAMENTAL_IMPLS,
206 "potentially-conflicting impls were erroneously allowed"
212 "detects use of deprecated items"
218 "unnecessary use of an `unsafe` block"
224 "detect mut variables which don't need to be mutable"
228 pub SINGLE_USE_LIFETIME,
230 "detects lifetime parameters that are only used once"
236 "detects lifetime parameters that are never used"
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_NOT_STARTING_WITH_CRATE,
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"
279 "detects labels that are never used"
282 /// Does nothing as a lint pass, but registers some `Lint`s
283 /// which are used by other parts of the compiler.
284 #[derive(Copy, Clone)]
285 pub struct HardwiredLints;
287 impl LintPass for HardwiredLints {
288 fn get_lints(&self) -> LintArray {
290 ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
293 UNUSED_EXTERN_CRATES,
294 UNUSED_QUALIFICATIONS,
300 UNREACHABLE_PATTERNS,
307 TRIVIAL_NUMERIC_CASTS,
309 PUB_USE_OF_PRIVATE_EXTERN_CRATE,
310 INVALID_TYPE_PARAM_DEFAULT,
312 RENAMED_AND_REMOVED_LINTS,
315 PATTERNS_IN_FNS_WITHOUT_BODY,
316 LEGACY_DIRECTORY_OWNERSHIP,
317 LEGACY_CONSTRUCTOR_VISIBILITY,
318 MISSING_FRAGMENT_SPECIFIER,
319 PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
320 LATE_BOUND_LIFETIME_ARGUMENTS,
321 INCOHERENT_FUNDAMENTAL_IMPLS,
328 TYVAR_BEHIND_RAW_POINTER,
329 ELIDED_LIFETIME_IN_PATH,
331 ABSOLUTE_PATH_NOT_STARTING_WITH_CRATE,
332 UNSTABLE_NAME_COLLISION,
337 // this could be a closure, but then implementing derive traits
338 // becomes hacky (and it gets allocated)
339 #[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
340 pub enum BuiltinLintDiagnostics {
342 BareTraitObject(Span, /* is_global */ bool),
343 AbsPathWithModule(Span),
346 impl BuiltinLintDiagnostics {
347 pub fn run(self, sess: &Session, db: &mut DiagnosticBuilder) {
349 BuiltinLintDiagnostics::Normal => (),
350 BuiltinLintDiagnostics::BareTraitObject(span, is_global) => {
351 let (sugg, app) = match sess.codemap().span_to_snippet(span) {
352 Ok(ref s) if is_global => (format!("dyn ({})", s),
353 Applicability::MachineApplicable),
354 Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
355 Err(_) => (format!("dyn <type>"), Applicability::HasPlaceholders)
357 db.span_suggestion_with_applicability(span, "use `dyn`", sugg, app);
359 BuiltinLintDiagnostics::AbsPathWithModule(span) => {
360 let (sugg, app) = match sess.codemap().span_to_snippet(span) {
362 // FIXME(Manishearth) ideally the emitting code
363 // can tell us whether or not this is global
364 let opt_colon = if s.trim_left().starts_with("::") {
370 (format!("crate{}{}", opt_colon, s), Applicability::MachineApplicable)
372 Err(_) => (format!("crate::<path>"), Applicability::HasPlaceholders)
374 db.span_suggestion_with_applicability(span, "use `crate`", sugg, app);
380 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HardwiredLints {}