]> git.lizzy.rs Git - rust.git/blob - src/librustc/lint/builtin.rs
added function to check if lints belong to an external macro
[rust.git] / src / librustc / lint / builtin.rs
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.
4 //
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.
10
11 //! Some lints that are built in to the compiler.
12 //!
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`.
16
17 use errors::{Applicability, DiagnosticBuilder};
18 use lint::{LintPass, LateLintPass, LintArray};
19 use session::Session;
20 use syntax::ast;
21 use syntax::codemap::Span;
22
23 declare_lint! {
24     pub EXCEEDING_BITSHIFTS,
25     Deny,
26     "shift exceeds the type's number of bits"
27 }
28
29 declare_lint! {
30     pub CONST_ERR,
31     Deny,
32     "constant evaluation detected erroneous expression"
33 }
34
35 declare_lint! {
36     pub UNUSED_IMPORTS,
37     Warn,
38     "imports that are never used"
39 }
40
41 declare_lint! {
42     pub UNUSED_EXTERN_CRATES,
43     Allow,
44     "extern crates that are never used"
45 }
46
47 declare_lint! {
48     pub UNUSED_QUALIFICATIONS,
49     Allow,
50     "detects unnecessarily qualified names"
51 }
52
53 declare_lint! {
54     pub UNKNOWN_LINTS,
55     Warn,
56     "unrecognized lint attribute"
57 }
58
59 declare_lint! {
60     pub UNUSED_VARIABLES,
61     Warn,
62     "detect variables which are not used in any way"
63 }
64
65 declare_lint! {
66     pub UNUSED_ASSIGNMENTS,
67     Warn,
68     "detect assignments that will never be read"
69 }
70
71 declare_lint! {
72     pub DEAD_CODE,
73     Warn,
74     "detect unused, unexported items"
75 }
76
77 declare_lint! {
78     pub UNREACHABLE_CODE,
79     Warn,
80     "detects unreachable code paths"
81 }
82
83 declare_lint! {
84     pub UNREACHABLE_PATTERNS,
85     Warn,
86     "detects unreachable patterns"
87 }
88
89 declare_lint! {
90     pub UNUSED_MACROS,
91     Warn,
92     "detects macros that were not used"
93 }
94
95 declare_lint! {
96     pub WARNINGS,
97     Warn,
98     "mass-change the level for lints which produce warnings"
99 }
100
101 declare_lint! {
102     pub UNUSED_FEATURES,
103     Warn,
104     "unused or unknown features found in crate-level #[feature] directives"
105 }
106
107 declare_lint! {
108     pub STABLE_FEATURES,
109     Warn,
110     "stable features found in #[feature] directive"
111 }
112
113 declare_lint! {
114     pub UNKNOWN_CRATE_TYPES,
115     Deny,
116     "unknown crate type found in #[crate_type] directive"
117 }
118
119 declare_lint! {
120     pub TRIVIAL_CASTS,
121     Allow,
122     "detects trivial casts which could be removed"
123 }
124
125 declare_lint! {
126     pub TRIVIAL_NUMERIC_CASTS,
127     Allow,
128     "detects trivial casts of numeric types which could be removed"
129 }
130
131 declare_lint! {
132     pub PRIVATE_IN_PUBLIC,
133     Warn,
134     "detect private items in public interfaces not caught by the old implementation"
135 }
136
137 declare_lint! {
138     pub PUB_USE_OF_PRIVATE_EXTERN_CRATE,
139     Deny,
140     "detect public re-exports of private extern crates"
141 }
142
143 declare_lint! {
144     pub INVALID_TYPE_PARAM_DEFAULT,
145     Deny,
146     "type parameter default erroneously allowed in invalid location"
147 }
148
149 declare_lint! {
150     pub RENAMED_AND_REMOVED_LINTS,
151     Warn,
152     "lints that have been renamed or removed"
153 }
154
155 declare_lint! {
156     pub SAFE_EXTERN_STATICS,
157     Deny,
158     "safe access to extern statics was erroneously allowed"
159 }
160
161 declare_lint! {
162     pub SAFE_PACKED_BORROWS,
163     Warn,
164     "safe borrows of fields of packed structs were was erroneously allowed"
165 }
166
167 declare_lint! {
168     pub PATTERNS_IN_FNS_WITHOUT_BODY,
169     Warn,
170     "patterns in functions without body were erroneously allowed"
171 }
172
173 declare_lint! {
174     pub LEGACY_DIRECTORY_OWNERSHIP,
175     Deny,
176     "non-inline, non-`#[path]` modules (e.g. `mod foo;`) were erroneously allowed in some files \
177      not named `mod.rs`"
178 }
179
180 declare_lint! {
181     pub LEGACY_CONSTRUCTOR_VISIBILITY,
182     Deny,
183     "detects use of struct constructors that would be invisible with new visibility rules"
184 }
185
186 declare_lint! {
187     pub MISSING_FRAGMENT_SPECIFIER,
188     Deny,
189     "detects missing fragment specifiers in unused `macro_rules!` patterns"
190 }
191
192 declare_lint! {
193     pub PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
194     Deny,
195     "detects parenthesized generic parameters in type and module names"
196 }
197
198 declare_lint! {
199     pub LATE_BOUND_LIFETIME_ARGUMENTS,
200     Warn,
201     "detects generic lifetime arguments in path segments with late bound lifetime parameters"
202 }
203
204 declare_lint! {
205     pub INCOHERENT_FUNDAMENTAL_IMPLS,
206     Deny,
207     "potentially-conflicting impls were erroneously allowed"
208 }
209
210 declare_lint! {
211     pub BAD_REPR,
212     Warn,
213     "detects incorrect use of `repr` attribute"
214 }
215
216 declare_lint! {
217     pub DEPRECATED,
218     Warn,
219     "detects use of deprecated items"
220 }
221
222 declare_lint! {
223     pub UNUSED_UNSAFE,
224     Warn,
225     "unnecessary use of an `unsafe` block"
226 }
227
228 declare_lint! {
229     pub UNUSED_MUT,
230     Warn,
231     "detect mut variables which don't need to be mutable"
232 }
233
234 declare_lint! {
235     pub SINGLE_USE_LIFETIMES,
236     Allow,
237     "detects lifetime parameters that are only used once"
238 }
239
240 declare_lint! {
241     pub UNUSED_LIFETIMES,
242     Allow,
243     "detects lifetime parameters that are never used"
244 }
245
246 declare_lint! {
247     pub TYVAR_BEHIND_RAW_POINTER,
248     Warn,
249     "raw pointer to an inference variable"
250 }
251
252 declare_lint! {
253     pub ELIDED_LIFETIMES_IN_PATHS,
254     Allow,
255     "hidden lifetime parameters are deprecated, try `Foo<'_>`"
256 }
257
258 declare_lint! {
259     pub BARE_TRAIT_OBJECTS,
260     Allow,
261     "suggest using `dyn Trait` for trait objects"
262 }
263
264 declare_lint! {
265     pub ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
266     Allow,
267     "fully qualified paths that start with a module name \
268      instead of `crate`, `self`, or an extern crate name"
269 }
270
271 declare_lint! {
272     pub ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
273     Warn,
274     "floating-point literals cannot be used in patterns"
275 }
276
277 declare_lint! {
278     pub UNSTABLE_NAME_COLLISIONS,
279     Warn,
280     "detects name collision with an existing but unstable method"
281 }
282
283 declare_lint! {
284     pub IRREFUTABLE_LET_PATTERNS,
285     Deny,
286     "detects irrefutable patterns in if-let and while-let statements"
287 }
288
289 declare_lint! {
290     pub UNUSED_LABELS,
291     Allow,
292     "detects labels that are never used"
293 }
294
295 declare_lint! {
296     pub DUPLICATE_ASSOCIATED_TYPE_BINDINGS,
297     Warn,
298     "warns about duplicate associated type bindings in generics"
299 }
300
301 declare_lint! {
302     pub DUPLICATE_MACRO_EXPORTS,
303     Deny,
304     "detects duplicate macro exports"
305 }
306
307 declare_lint! {
308     pub INTRA_DOC_LINK_RESOLUTION_FAILURE,
309     Warn,
310     "warn about documentation intra links resolution failure"
311 }
312
313 declare_lint! {
314     pub WHERE_CLAUSES_OBJECT_SAFETY,
315     Warn,
316     "checks the object safety of where clauses"
317 }
318
319 declare_lint! {
320     pub PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
321     Warn,
322     "detects proc macro derives using inaccessible names from parent modules"
323 }
324
325 declare_lint! {
326     pub MACRO_USE_EXTERN_CRATE,
327     Allow,
328     "the `#[macro_use]` attribute is now deprecated in favor of using macros \
329      via the module system"
330 }
331
332 /// Does nothing as a lint pass, but registers some `Lint`s
333 /// which are used by other parts of the compiler.
334 #[derive(Copy, Clone)]
335 pub struct HardwiredLints;
336
337 impl LintPass for HardwiredLints {
338     fn get_lints(&self) -> LintArray {
339         lint_array!(
340             ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
341             EXCEEDING_BITSHIFTS,
342             UNUSED_IMPORTS,
343             UNUSED_EXTERN_CRATES,
344             UNUSED_QUALIFICATIONS,
345             UNKNOWN_LINTS,
346             UNUSED_VARIABLES,
347             UNUSED_ASSIGNMENTS,
348             DEAD_CODE,
349             UNREACHABLE_CODE,
350             UNREACHABLE_PATTERNS,
351             UNUSED_MACROS,
352             WARNINGS,
353             UNUSED_FEATURES,
354             STABLE_FEATURES,
355             UNKNOWN_CRATE_TYPES,
356             TRIVIAL_CASTS,
357             TRIVIAL_NUMERIC_CASTS,
358             PRIVATE_IN_PUBLIC,
359             PUB_USE_OF_PRIVATE_EXTERN_CRATE,
360             INVALID_TYPE_PARAM_DEFAULT,
361             CONST_ERR,
362             RENAMED_AND_REMOVED_LINTS,
363             SAFE_EXTERN_STATICS,
364             SAFE_PACKED_BORROWS,
365             PATTERNS_IN_FNS_WITHOUT_BODY,
366             LEGACY_DIRECTORY_OWNERSHIP,
367             LEGACY_CONSTRUCTOR_VISIBILITY,
368             MISSING_FRAGMENT_SPECIFIER,
369             PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
370             LATE_BOUND_LIFETIME_ARGUMENTS,
371             INCOHERENT_FUNDAMENTAL_IMPLS,
372             DEPRECATED,
373             UNUSED_UNSAFE,
374             UNUSED_MUT,
375             SINGLE_USE_LIFETIMES,
376             UNUSED_LIFETIMES,
377             UNUSED_LABELS,
378             TYVAR_BEHIND_RAW_POINTER,
379             ELIDED_LIFETIMES_IN_PATHS,
380             BARE_TRAIT_OBJECTS,
381             ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
382             UNSTABLE_NAME_COLLISIONS,
383             IRREFUTABLE_LET_PATTERNS,
384             DUPLICATE_ASSOCIATED_TYPE_BINDINGS,
385             DUPLICATE_MACRO_EXPORTS,
386             INTRA_DOC_LINK_RESOLUTION_FAILURE,
387             WHERE_CLAUSES_OBJECT_SAFETY,
388             PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
389             MACRO_USE_EXTERN_CRATE,
390         )
391     }
392 }
393
394 // this could be a closure, but then implementing derive traits
395 // becomes hacky (and it gets allocated)
396 #[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
397 pub enum BuiltinLintDiagnostics {
398     Normal,
399     BareTraitObject(Span, /* is_global */ bool),
400     AbsPathWithModule(Span),
401     DuplicatedMacroExports(ast::Ident, Span, Span),
402     ProcMacroDeriveResolutionFallback(Span),
403 }
404
405 impl BuiltinLintDiagnostics {
406     pub fn run(self, sess: &Session, db: &mut DiagnosticBuilder) {
407         match self {
408             BuiltinLintDiagnostics::Normal => (),
409             BuiltinLintDiagnostics::BareTraitObject(span, is_global) => {
410                 let (sugg, app) = match sess.codemap().span_to_snippet(span) {
411                     Ok(ref s) if is_global => (format!("dyn ({})", s),
412                                                Applicability::MachineApplicable),
413                     Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
414                     Err(_) => (format!("dyn <type>"), Applicability::HasPlaceholders)
415                 };
416                 db.span_suggestion_with_applicability(span, "use `dyn`", sugg, app);
417             }
418             BuiltinLintDiagnostics::AbsPathWithModule(span) => {
419                 let (sugg, app) = match sess.codemap().span_to_snippet(span) {
420                     Ok(ref s) => {
421                         // FIXME(Manishearth) ideally the emitting code
422                         // can tell us whether or not this is global
423                         let opt_colon = if s.trim_left().starts_with("::") {
424                             ""
425                         } else {
426                             "::"
427                         };
428
429                         (format!("crate{}{}", opt_colon, s), Applicability::MachineApplicable)
430                     }
431                     Err(_) => (format!("crate::<path>"), Applicability::HasPlaceholders)
432                 };
433                 db.span_suggestion_with_applicability(span, "use `crate`", sugg, app);
434             }
435             BuiltinLintDiagnostics::DuplicatedMacroExports(ident, earlier_span, later_span) => {
436                 db.span_label(later_span, format!("`{}` already exported", ident));
437                 db.span_note(earlier_span, "previous macro export is now shadowed");
438             }
439             BuiltinLintDiagnostics::ProcMacroDeriveResolutionFallback(span) => {
440                 db.span_label(span, "names from parent modules are not \
441                                      accessible without an explicit import");
442             }
443         }
444     }
445 }
446
447 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HardwiredLints {}