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};
21 use syntax::codemap::Span;
24 pub EXCEEDING_BITSHIFTS,
26 "shift exceeds the type's number of bits"
32 "constant evaluation detected erroneous expression"
38 "imports that are never used"
42 pub UNUSED_EXTERN_CRATES,
44 "extern crates that are never used"
48 pub UNUSED_QUALIFICATIONS,
50 "detects unnecessarily qualified names"
56 "unrecognized lint attribute"
62 "detect variables which are not used in any way"
66 pub UNUSED_ASSIGNMENTS,
68 "detect assignments that will never be read"
74 "detect unused, unexported items"
80 "detects unreachable code paths"
84 pub UNREACHABLE_PATTERNS,
86 "detects unreachable patterns"
92 "detects macros that were not used"
98 "mass-change the level for lints which produce warnings"
104 "unused or unknown features found in crate-level #[feature] directives"
110 "stable features found in #[feature] directive"
114 pub UNKNOWN_CRATE_TYPES,
116 "unknown crate type found in #[crate_type] directive"
122 "detects trivial casts which could be removed"
126 pub TRIVIAL_NUMERIC_CASTS,
128 "detects trivial casts of numeric types which could be removed"
132 pub PRIVATE_IN_PUBLIC,
134 "detect private items in public interfaces not caught by the old implementation"
138 pub PUB_USE_OF_PRIVATE_EXTERN_CRATE,
140 "detect public re-exports of private extern crates"
144 pub INVALID_TYPE_PARAM_DEFAULT,
146 "type parameter default erroneously allowed in invalid location"
150 pub RENAMED_AND_REMOVED_LINTS,
152 "lints that have been renamed or removed"
156 pub SAFE_EXTERN_STATICS,
158 "safe access to extern statics was erroneously allowed"
162 pub SAFE_PACKED_BORROWS,
164 "safe borrows of fields of packed structs were was erroneously allowed"
168 pub PATTERNS_IN_FNS_WITHOUT_BODY,
170 "patterns in functions without body were erroneously allowed"
174 pub LEGACY_DIRECTORY_OWNERSHIP,
176 "non-inline, non-`#[path]` modules (e.g. `mod foo;`) were erroneously allowed in some files \
181 pub LEGACY_CONSTRUCTOR_VISIBILITY,
183 "detects use of struct constructors that would be invisible with new visibility rules"
187 pub MISSING_FRAGMENT_SPECIFIER,
189 "detects missing fragment specifiers in unused `macro_rules!` patterns"
193 pub PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
195 "detects parenthesized generic parameters in type and module names"
199 pub LATE_BOUND_LIFETIME_ARGUMENTS,
201 "detects generic lifetime arguments in path segments with late bound lifetime parameters"
205 pub INCOHERENT_FUNDAMENTAL_IMPLS,
207 "potentially-conflicting impls were erroneously allowed"
213 "detects incorrect use of `repr` attribute"
219 "detects use of deprecated items"
225 "unnecessary use of an `unsafe` block"
231 "detect mut variables which don't need to be mutable"
235 pub SINGLE_USE_LIFETIMES,
237 "detects lifetime parameters that are only used once"
241 pub UNUSED_LIFETIMES,
243 "detects lifetime parameters that are never used"
247 pub TYVAR_BEHIND_RAW_POINTER,
249 "raw pointer to an inference variable"
253 pub ELIDED_LIFETIMES_IN_PATHS,
255 "hidden lifetime parameters are deprecated, try `Foo<'_>`"
259 pub BARE_TRAIT_OBJECTS,
261 "suggest using `dyn Trait` for trait objects"
265 pub ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
267 "fully qualified paths that start with a module name \
268 instead of `crate`, `self`, or an extern crate name"
272 pub ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
274 "floating-point literals cannot be used in patterns"
278 pub UNSTABLE_NAME_COLLISIONS,
280 "detects name collision with an existing but unstable method"
284 pub IRREFUTABLE_LET_PATTERNS,
286 "detects irrefutable patterns in if-let and while-let statements"
292 "detects labels that are never used"
296 pub DUPLICATE_ASSOCIATED_TYPE_BINDINGS,
298 "warns about duplicate associated type bindings in generics"
302 pub DUPLICATE_MACRO_EXPORTS,
304 "detects duplicate macro exports"
308 pub INTRA_DOC_LINK_RESOLUTION_FAILURE,
310 "warn about documentation intra links resolution failure"
314 pub WHERE_CLAUSES_OBJECT_SAFETY,
316 "checks the object safety of where clauses"
319 /// Does nothing as a lint pass, but registers some `Lint`s
320 /// which are used by other parts of the compiler.
321 #[derive(Copy, Clone)]
322 pub struct HardwiredLints;
324 impl LintPass for HardwiredLints {
325 fn get_lints(&self) -> LintArray {
327 ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
330 UNUSED_EXTERN_CRATES,
331 UNUSED_QUALIFICATIONS,
337 UNREACHABLE_PATTERNS,
344 TRIVIAL_NUMERIC_CASTS,
346 PUB_USE_OF_PRIVATE_EXTERN_CRATE,
347 INVALID_TYPE_PARAM_DEFAULT,
349 RENAMED_AND_REMOVED_LINTS,
352 PATTERNS_IN_FNS_WITHOUT_BODY,
353 LEGACY_DIRECTORY_OWNERSHIP,
354 LEGACY_CONSTRUCTOR_VISIBILITY,
355 MISSING_FRAGMENT_SPECIFIER,
356 PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
357 LATE_BOUND_LIFETIME_ARGUMENTS,
358 INCOHERENT_FUNDAMENTAL_IMPLS,
362 SINGLE_USE_LIFETIMES,
365 TYVAR_BEHIND_RAW_POINTER,
366 ELIDED_LIFETIMES_IN_PATHS,
368 ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
369 UNSTABLE_NAME_COLLISIONS,
370 IRREFUTABLE_LET_PATTERNS,
371 DUPLICATE_ASSOCIATED_TYPE_BINDINGS,
372 DUPLICATE_MACRO_EXPORTS,
373 INTRA_DOC_LINK_RESOLUTION_FAILURE,
374 WHERE_CLAUSES_OBJECT_SAFETY,
379 // this could be a closure, but then implementing derive traits
380 // becomes hacky (and it gets allocated)
381 #[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
382 pub enum BuiltinLintDiagnostics {
384 BareTraitObject(Span, /* is_global */ bool),
385 AbsPathWithModule(Span),
386 DuplicatedMacroExports(ast::Ident, Span, Span),
389 impl BuiltinLintDiagnostics {
390 pub fn run(self, sess: &Session, db: &mut DiagnosticBuilder) {
392 BuiltinLintDiagnostics::Normal => (),
393 BuiltinLintDiagnostics::BareTraitObject(span, is_global) => {
394 let (sugg, app) = match sess.codemap().span_to_snippet(span) {
395 Ok(ref s) if is_global => (format!("dyn ({})", s),
396 Applicability::MachineApplicable),
397 Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
398 Err(_) => (format!("dyn <type>"), Applicability::HasPlaceholders)
400 db.span_suggestion_with_applicability(span, "use `dyn`", sugg, app);
402 BuiltinLintDiagnostics::AbsPathWithModule(span) => {
403 let (sugg, app) = match sess.codemap().span_to_snippet(span) {
405 // FIXME(Manishearth) ideally the emitting code
406 // can tell us whether or not this is global
407 let opt_colon = if s.trim_left().starts_with("::") {
413 (format!("crate{}{}", opt_colon, s), Applicability::MachineApplicable)
415 Err(_) => (format!("crate::<path>"), Applicability::HasPlaceholders)
417 db.span_suggestion_with_applicability(span, "use `crate`", sugg, app);
419 BuiltinLintDiagnostics::DuplicatedMacroExports(ident, earlier_span, later_span) => {
420 db.span_label(later_span, format!("`{}` already exported", ident));
421 db.span_note(earlier_span, "previous macro export is now shadowed");
427 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HardwiredLints {}