]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/lib.rs
Deny `internal` in stage0
[rust.git] / src / librustc_lint / lib.rs
1 //! # Lints in the Rust compiler
2 //!
3 //! This currently only contains the definitions and implementations
4 //! of most of the lints that `rustc` supports directly, it does not
5 //! contain the infrastructure for defining/registering lints. That is
6 //! available in `rustc::lint` and `rustc_plugin` respectively.
7 //!
8 //! ## Note
9 //!
10 //! This API is completely unstable and subject to change.
11
12 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
13
14 #![cfg_attr(test, feature(test))]
15 #![feature(box_patterns)]
16 #![feature(box_syntax)]
17 #![feature(nll)]
18 #![feature(rustc_diagnostic_macros)]
19
20 #![recursion_limit="256"]
21
22 #![deny(rust_2018_idioms)]
23 #![deny(internal)]
24
25 #[macro_use]
26 extern crate rustc;
27
28 mod diagnostics;
29 mod nonstandard_style;
30 pub mod builtin;
31 mod types;
32 mod unused;
33
34 use rustc::lint;
35 use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
36 use rustc::lint::builtin::{
37     BARE_TRAIT_OBJECTS,
38     ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
39     ELIDED_LIFETIMES_IN_PATHS,
40     EXPLICIT_OUTLIVES_REQUIREMENTS,
41     INTRA_DOC_LINK_RESOLUTION_FAILURE,
42     MISSING_DOC_CODE_EXAMPLES,
43     PRIVATE_DOC_TESTS,
44     parser::QUESTION_MARK_MACRO_SEP,
45     parser::ILL_FORMED_ATTRIBUTE_INPUT,
46 };
47 use rustc::session;
48 use rustc::hir;
49 use rustc::hir::def_id::DefId;
50 use rustc::ty::query::Providers;
51 use rustc::ty::TyCtxt;
52
53 use syntax::ast;
54 use syntax::edition::Edition;
55 use syntax_pos::Span;
56
57 use session::Session;
58 use lint::LintId;
59 use lint::FutureIncompatibleInfo;
60
61 use nonstandard_style::*;
62 use builtin::*;
63 use types::*;
64 use unused::*;
65 use rustc::lint::internal::*;
66
67 /// Useful for other parts of the compiler.
68 pub use builtin::SoftLints;
69
70 pub fn provide(providers: &mut Providers<'_>) {
71     *providers = Providers {
72         lint_mod,
73         ..*providers
74     };
75 }
76
77 fn lint_mod<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, module_def_id: DefId) {
78     lint::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
79 }
80
81 macro_rules! pre_expansion_lint_passes {
82     ($macro:path, $args:tt) => (
83         $macro!($args, [
84             KeywordIdents: KeywordIdents,
85             UnusedDocComment: UnusedDocComment,
86         ]);
87     )
88 }
89
90 macro_rules! early_lint_passes {
91     ($macro:path, $args:tt) => (
92         $macro!($args, [
93             UnusedParens: UnusedParens,
94             UnusedImportBraces: UnusedImportBraces,
95             UnsafeCode: UnsafeCode,
96             AnonymousParameters: AnonymousParameters,
97             EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns,
98             NonCamelCaseTypes: NonCamelCaseTypes,
99             DeprecatedAttr: DeprecatedAttr::new(),
100         ]);
101     )
102 }
103
104 macro_rules! declare_combined_early_pass {
105     ([$name:ident], $passes:tt) => (
106         early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
107     )
108 }
109
110 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
111 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
112
113 macro_rules! late_lint_passes {
114     ($macro:path, $args:tt) => (
115         $macro!($args, [
116             // FIXME: Look into regression when this is used as a module lint
117             // May Depend on constants elsewhere
118             UnusedBrokenConst: UnusedBrokenConst,
119
120             // Uses attr::is_used which is untracked, can't be an incremental module pass.
121             UnusedAttributes: UnusedAttributes::new(),
122
123             // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
124             UnstableFeatures: UnstableFeatures,
125
126             // Tracks state across modules
127             UnnameableTestItems: UnnameableTestItems::new(),
128
129             // Tracks attributes of parents
130             MissingDoc: MissingDoc::new(),
131
132             // Depends on access levels
133             // FIXME: Turn the computation of types which implement Debug into a query
134             // and change this to a module lint pass
135             MissingDebugImplementations: MissingDebugImplementations::new(),
136         ]);
137     )
138 }
139
140 macro_rules! late_lint_mod_passes {
141     ($macro:path, $args:tt) => (
142         $macro!($args, [
143             HardwiredLints: HardwiredLints,
144             WhileTrue: WhileTrue,
145             ImproperCTypes: ImproperCTypes,
146             VariantSizeDifferences: VariantSizeDifferences,
147             BoxPointers: BoxPointers,
148             PathStatements: PathStatements,
149
150             // Depends on referenced function signatures in expressions
151             UnusedResults: UnusedResults,
152
153             NonUpperCaseGlobals: NonUpperCaseGlobals,
154             NonShorthandFieldPatterns: NonShorthandFieldPatterns,
155             UnusedAllocation: UnusedAllocation,
156
157             // Depends on types used in type definitions
158             MissingCopyImplementations: MissingCopyImplementations,
159
160             PluginAsLibrary: PluginAsLibrary,
161
162             // Depends on referenced function signatures in expressions
163             MutableTransmutes: MutableTransmutes,
164
165             // Depends on types of fields, checks if they implement Drop
166             UnionsWithDropFields: UnionsWithDropFields,
167
168             TypeAliasBounds: TypeAliasBounds,
169
170             TrivialConstraints: TrivialConstraints,
171             TypeLimits: TypeLimits::new(),
172
173             NonSnakeCase: NonSnakeCase,
174             InvalidNoMangleItems: InvalidNoMangleItems,
175
176             // Depends on access levels
177             UnreachablePub: UnreachablePub,
178
179             ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
180         ]);
181     )
182 }
183
184 macro_rules! declare_combined_late_pass {
185     ([$v:vis $name:ident], $passes:tt) => (
186         late_lint_methods!(declare_combined_late_lint_pass, [$v $name, $passes], ['tcx]);
187     )
188 }
189
190 // FIXME: Make a separate lint type which do not require typeck tables
191 late_lint_passes!(declare_combined_late_pass, [pub BuiltinCombinedLateLintPass]);
192
193 late_lint_mod_passes!(declare_combined_late_pass, [BuiltinCombinedModuleLateLintPass]);
194
195 /// Tell the `LintStore` about all the built-in lints (the ones
196 /// defined in this crate and the ones defined in
197 /// `rustc::lint::builtin`).
198 pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
199     macro_rules! add_lint_group {
200         ($sess:ident, $name:expr, $($lint:ident),*) => (
201             store.register_group($sess, false, $name, None, vec![$(LintId::of($lint)),*]);
202         )
203     }
204
205     macro_rules! register_pass {
206         ($method:ident, $constructor:expr, [$($args:expr),*]) => (
207             store.$method(sess, false, false, $($args,)* box $constructor);
208         )
209     }
210
211     macro_rules! register_passes {
212         ([$method:ident, $args:tt], [$($passes:ident: $constructor:expr,)*]) => (
213             $(
214                 register_pass!($method, $constructor, $args);
215             )*
216         )
217     }
218
219     if sess.map(|sess| sess.opts.debugging_opts.no_interleave_lints).unwrap_or(false) {
220         pre_expansion_lint_passes!(register_passes, [register_pre_expansion_pass, []]);
221         early_lint_passes!(register_passes, [register_early_pass, []]);
222         late_lint_passes!(register_passes, [register_late_pass, [false]]);
223         late_lint_mod_passes!(register_passes, [register_late_pass, [true]]);
224     } else {
225         store.register_pre_expansion_pass(
226             sess,
227             false,
228             true,
229             box BuiltinCombinedPreExpansionLintPass::new()
230         );
231         store.register_early_pass(sess, false, true, box BuiltinCombinedEarlyLintPass::new());
232         store.register_late_pass(
233             sess, false, true, true, box BuiltinCombinedModuleLateLintPass::new()
234         );
235         store.register_late_pass(
236             sess, false, true, false, box BuiltinCombinedLateLintPass::new()
237         );
238     }
239
240     add_lint_group!(sess,
241                     "nonstandard_style",
242                     NON_CAMEL_CASE_TYPES,
243                     NON_SNAKE_CASE,
244                     NON_UPPER_CASE_GLOBALS);
245
246     add_lint_group!(sess,
247                     "unused",
248                     UNUSED_IMPORTS,
249                     UNUSED_VARIABLES,
250                     UNUSED_ASSIGNMENTS,
251                     DEAD_CODE,
252                     UNUSED_MUT,
253                     UNREACHABLE_CODE,
254                     UNREACHABLE_PATTERNS,
255                     UNUSED_MUST_USE,
256                     UNUSED_UNSAFE,
257                     PATH_STATEMENTS,
258                     UNUSED_ATTRIBUTES,
259                     UNUSED_MACROS,
260                     UNUSED_ALLOCATION,
261                     UNUSED_DOC_COMMENTS,
262                     UNUSED_EXTERN_CRATES,
263                     UNUSED_FEATURES,
264                     UNUSED_LABELS,
265                     UNUSED_PARENS);
266
267     add_lint_group!(sess,
268                     "rust_2018_idioms",
269                     BARE_TRAIT_OBJECTS,
270                     UNUSED_EXTERN_CRATES,
271                     ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
272                     ELIDED_LIFETIMES_IN_PATHS,
273                     EXPLICIT_OUTLIVES_REQUIREMENTS
274
275                     // FIXME(#52665, #47816) not always applicable and not all
276                     // macros are ready for this yet.
277                     // UNREACHABLE_PUB,
278
279                     // FIXME macro crates are not up for this yet, too much
280                     // breakage is seen if we try to encourage this lint.
281                     // MACRO_USE_EXTERN_CRATE,
282                     );
283
284     add_lint_group!(sess,
285                     "rustdoc",
286                     INTRA_DOC_LINK_RESOLUTION_FAILURE,
287                     MISSING_DOC_CODE_EXAMPLES,
288                     PRIVATE_DOC_TESTS);
289
290     // Guidelines for creating a future incompatibility lint:
291     //
292     // - Create a lint defaulting to warn as normal, with ideally the same error
293     //   message you would normally give
294     // - Add a suitable reference, typically an RFC or tracking issue. Go ahead
295     //   and include the full URL, sort items in ascending order of issue numbers.
296     // - Later, change lint to error
297     // - Eventually, remove lint
298     store.register_future_incompatible(sess, vec![
299         FutureIncompatibleInfo {
300             id: LintId::of(PRIVATE_IN_PUBLIC),
301             reference: "issue #34537 <https://github.com/rust-lang/rust/issues/34537>",
302             edition: None,
303         },
304         FutureIncompatibleInfo {
305             id: LintId::of(PUB_USE_OF_PRIVATE_EXTERN_CRATE),
306             reference: "issue #34537 <https://github.com/rust-lang/rust/issues/34537>",
307             edition: None,
308         },
309         FutureIncompatibleInfo {
310             id: LintId::of(PATTERNS_IN_FNS_WITHOUT_BODY),
311             reference: "issue #35203 <https://github.com/rust-lang/rust/issues/35203>",
312             edition: None,
313         },
314         FutureIncompatibleInfo {
315             id: LintId::of(DUPLICATE_MACRO_EXPORTS),
316             reference: "issue #35896 <https://github.com/rust-lang/rust/issues/35896>",
317             edition: Some(Edition::Edition2018),
318         },
319         FutureIncompatibleInfo {
320             id: LintId::of(KEYWORD_IDENTS),
321             reference: "issue #49716 <https://github.com/rust-lang/rust/issues/49716>",
322             edition: Some(Edition::Edition2018),
323         },
324         FutureIncompatibleInfo {
325             id: LintId::of(SAFE_EXTERN_STATICS),
326             reference: "issue #36247 <https://github.com/rust-lang/rust/issues/36247>",
327             edition: None,
328         },
329         FutureIncompatibleInfo {
330             id: LintId::of(INVALID_TYPE_PARAM_DEFAULT),
331             reference: "issue #36887 <https://github.com/rust-lang/rust/issues/36887>",
332             edition: None,
333         },
334         FutureIncompatibleInfo {
335             id: LintId::of(LEGACY_DIRECTORY_OWNERSHIP),
336             reference: "issue #37872 <https://github.com/rust-lang/rust/issues/37872>",
337             edition: None,
338         },
339         FutureIncompatibleInfo {
340             id: LintId::of(LEGACY_CONSTRUCTOR_VISIBILITY),
341             reference: "issue #39207 <https://github.com/rust-lang/rust/issues/39207>",
342             edition: None,
343         },
344         FutureIncompatibleInfo {
345             id: LintId::of(MISSING_FRAGMENT_SPECIFIER),
346             reference: "issue #40107 <https://github.com/rust-lang/rust/issues/40107>",
347             edition: None,
348         },
349         FutureIncompatibleInfo {
350             id: LintId::of(ILLEGAL_FLOATING_POINT_LITERAL_PATTERN),
351             reference: "issue #41620 <https://github.com/rust-lang/rust/issues/41620>",
352             edition: None,
353         },
354         FutureIncompatibleInfo {
355             id: LintId::of(ANONYMOUS_PARAMETERS),
356             reference: "issue #41686 <https://github.com/rust-lang/rust/issues/41686>",
357             edition: Some(Edition::Edition2018),
358         },
359         FutureIncompatibleInfo {
360             id: LintId::of(PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES),
361             reference: "issue #42238 <https://github.com/rust-lang/rust/issues/42238>",
362             edition: None,
363         },
364         FutureIncompatibleInfo {
365             id: LintId::of(LATE_BOUND_LIFETIME_ARGUMENTS),
366             reference: "issue #42868 <https://github.com/rust-lang/rust/issues/42868>",
367             edition: None,
368         },
369         FutureIncompatibleInfo {
370             id: LintId::of(SAFE_PACKED_BORROWS),
371             reference: "issue #46043 <https://github.com/rust-lang/rust/issues/46043>",
372             edition: None,
373         },
374         FutureIncompatibleInfo {
375             id: LintId::of(INCOHERENT_FUNDAMENTAL_IMPLS),
376             reference: "issue #46205 <https://github.com/rust-lang/rust/issues/46205>",
377             edition: None,
378         },
379         FutureIncompatibleInfo {
380             id: LintId::of(ORDER_DEPENDENT_TRAIT_OBJECTS),
381             reference: "issue #56484 <https://github.com/rust-lang/rust/issues/56484>",
382             edition: None,
383         },
384         FutureIncompatibleInfo {
385             id: LintId::of(TYVAR_BEHIND_RAW_POINTER),
386             reference: "issue #46906 <https://github.com/rust-lang/rust/issues/46906>",
387             edition: Some(Edition::Edition2018),
388         },
389         FutureIncompatibleInfo {
390             id: LintId::of(UNSTABLE_NAME_COLLISIONS),
391             reference: "issue #48919 <https://github.com/rust-lang/rust/issues/48919>",
392             edition: None,
393             // Note: this item represents future incompatibility of all unstable functions in the
394             //       standard library, and thus should never be removed or changed to an error.
395         },
396         FutureIncompatibleInfo {
397             id: LintId::of(ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE),
398             reference: "issue #53130 <https://github.com/rust-lang/rust/issues/53130>",
399             edition: Some(Edition::Edition2018),
400         },
401         FutureIncompatibleInfo {
402             id: LintId::of(WHERE_CLAUSES_OBJECT_SAFETY),
403             reference: "issue #51443 <https://github.com/rust-lang/rust/issues/51443>",
404             edition: None,
405         },
406         FutureIncompatibleInfo {
407             id: LintId::of(PROC_MACRO_DERIVE_RESOLUTION_FALLBACK),
408             reference: "issue #50504 <https://github.com/rust-lang/rust/issues/50504>",
409             edition: None,
410         },
411         FutureIncompatibleInfo {
412             id: LintId::of(QUESTION_MARK_MACRO_SEP),
413             reference: "issue #48075 <https://github.com/rust-lang/rust/issues/48075>",
414             edition: Some(Edition::Edition2018),
415         },
416         FutureIncompatibleInfo {
417             id: LintId::of(MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS),
418             reference: "issue #52234 <https://github.com/rust-lang/rust/issues/52234>",
419             edition: None,
420         },
421         FutureIncompatibleInfo {
422             id: LintId::of(ILL_FORMED_ATTRIBUTE_INPUT),
423             reference: "issue #57571 <https://github.com/rust-lang/rust/issues/57571>",
424             edition: None,
425         },
426         FutureIncompatibleInfo {
427             id: LintId::of(AMBIGUOUS_ASSOCIATED_ITEMS),
428             reference: "issue #57644 <https://github.com/rust-lang/rust/issues/57644>",
429             edition: None,
430         },
431         FutureIncompatibleInfo {
432             id: LintId::of(NESTED_IMPL_TRAIT),
433             reference: "issue #59014 <https://github.com/rust-lang/rust/issues/59014>",
434             edition: None,
435         },
436         FutureIncompatibleInfo {
437             id: LintId::of(MUTABLE_BORROW_RESERVATION_CONFLICT),
438             reference: "issue #59159 <https://github.com/rust-lang/rust/issues/59159>",
439             edition: None,
440         }
441         ]);
442
443     // Register renamed and removed lints.
444     store.register_renamed("single_use_lifetime", "single_use_lifetimes");
445     store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
446     store.register_renamed("bare_trait_object", "bare_trait_objects");
447     store.register_renamed("unstable_name_collision", "unstable_name_collisions");
448     store.register_renamed("unused_doc_comment", "unused_doc_comments");
449     store.register_renamed("async_idents", "keyword_idents");
450     store.register_removed("unknown_features", "replaced by an error");
451     store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
452     store.register_removed("negate_unsigned", "cast a signed value instead");
453     store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
454     // Register lint group aliases.
455     store.register_group_alias("nonstandard_style", "bad_style");
456     // This was renamed to `raw_pointer_derive`, which was then removed,
457     // so it is also considered removed.
458     store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
459     store.register_removed("drop_with_repr_extern", "drop flags have been removed");
460     store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
461     store.register_removed("deprecated_attr", "use `deprecated` instead");
462     store.register_removed("transmute_from_fn_item_types",
463         "always cast functions before transmuting them");
464     store.register_removed("hr_lifetime_in_assoc_type",
465         "converted into hard error, see https://github.com/rust-lang/rust/issues/33685");
466     store.register_removed("inaccessible_extern_crate",
467         "converted into hard error, see https://github.com/rust-lang/rust/issues/36886");
468     store.register_removed("super_or_self_in_global_path",
469         "converted into hard error, see https://github.com/rust-lang/rust/issues/36888");
470     store.register_removed("overlapping_inherent_impls",
471         "converted into hard error, see https://github.com/rust-lang/rust/issues/36889");
472     store.register_removed("illegal_floating_point_constant_pattern",
473         "converted into hard error, see https://github.com/rust-lang/rust/issues/36890");
474     store.register_removed("illegal_struct_or_enum_constant_pattern",
475         "converted into hard error, see https://github.com/rust-lang/rust/issues/36891");
476     store.register_removed("lifetime_underscore",
477         "converted into hard error, see https://github.com/rust-lang/rust/issues/36892");
478     store.register_removed("extra_requirement_in_impl",
479         "converted into hard error, see https://github.com/rust-lang/rust/issues/37166");
480     store.register_removed("legacy_imports",
481         "converted into hard error, see https://github.com/rust-lang/rust/issues/38260");
482     store.register_removed("coerce_never",
483         "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
484     store.register_removed("resolve_trait_on_defaulted_unit",
485         "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
486     store.register_removed("private_no_mangle_fns",
487         "no longer a warning, #[no_mangle] functions always exported");
488     store.register_removed("private_no_mangle_statics",
489         "no longer a warning, #[no_mangle] statics always exported");
490     store.register_removed("bad_repr",
491         "replaced with a generic attribute input check");
492     store.register_removed("duplicate_matcher_binding_name",
493         "converted into hard error, see https://github.com/rust-lang/rust/issues/57742");
494 }
495
496 pub fn register_internals(store: &mut lint::LintStore, sess: Option<&Session>) {
497     store.register_early_pass(sess, false, false, box DefaultHashTypes::new());
498     store.register_late_pass(sess, false, false, false, box TyKindUsage);
499     store.register_group(
500         sess,
501         false,
502         "internal",
503         None,
504         vec![
505             LintId::of(DEFAULT_HASH_TYPES),
506             LintId::of(USAGE_OF_TY_TYKIND),
507         ],
508     );
509 }