1 //! # Lints in the Rust compiler
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_driver::plugin` respectively.
10 //! This API is completely unstable and subject to change.
12 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
13 #![cfg_attr(test, feature(test))]
14 #![feature(bool_to_option)]
15 #![feature(box_patterns)]
16 #![feature(box_syntax)]
18 #![feature(matches_macro)]
19 #![recursion_limit = "256"]
24 extern crate rustc_session;
29 mod nonstandard_style;
30 mod redundant_semicolon;
35 use rustc::hir::def_id::DefId;
37 use rustc::lint::builtin::{
38 BARE_TRAIT_OBJECTS, ELIDED_LIFETIMES_IN_PATHS, EXPLICIT_OUTLIVES_REQUIREMENTS,
39 INTRA_DOC_LINK_RESOLUTION_FAILURE, MISSING_DOC_CODE_EXAMPLES, PRIVATE_DOC_TESTS,
41 use rustc::lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintPass};
42 use rustc::ty::query::Providers;
43 use rustc::ty::TyCtxt;
50 use array_into_iter::ArrayIntoIter;
52 use non_ascii_idents::*;
53 use nonstandard_style::*;
54 use redundant_semicolon::*;
55 use rustc::lint::internal::*;
59 /// Useful for other parts of the compiler.
60 pub use builtin::SoftLints;
62 pub fn provide(providers: &mut Providers<'_>) {
63 *providers = Providers { lint_mod, ..*providers };
66 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
67 lint::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
70 macro_rules! pre_expansion_lint_passes {
71 ($macro:path, $args:tt) => {
72 $macro!($args, [KeywordIdents: KeywordIdents, UnusedDocComment: UnusedDocComment,]);
76 macro_rules! early_lint_passes {
77 ($macro:path, $args:tt) => {
81 UnusedParens: UnusedParens,
82 UnusedImportBraces: UnusedImportBraces,
83 UnsafeCode: UnsafeCode,
84 AnonymousParameters: AnonymousParameters,
85 EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
86 NonCamelCaseTypes: NonCamelCaseTypes,
87 DeprecatedAttr: DeprecatedAttr::new(),
89 NonAsciiIdents: NonAsciiIdents,
90 IncompleteFeatures: IncompleteFeatures,
91 RedundantSemicolon: RedundantSemicolon,
97 macro_rules! declare_combined_early_pass {
98 ([$name:ident], $passes:tt) => (
99 early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
103 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
104 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
106 macro_rules! late_lint_passes {
107 ($macro:path, $args:tt) => {
111 // FIXME: Look into regression when this is used as a module lint
112 // May Depend on constants elsewhere
113 UnusedBrokenConst: UnusedBrokenConst,
114 // Uses attr::is_used which is untracked, can't be an incremental module pass.
115 UnusedAttributes: UnusedAttributes::new(),
116 // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
117 UnstableFeatures: UnstableFeatures,
118 // Tracks state across modules
119 UnnameableTestItems: UnnameableTestItems::new(),
120 // Tracks attributes of parents
121 MissingDoc: MissingDoc::new(),
122 // Depends on access levels
123 // FIXME: Turn the computation of types which implement Debug into a query
124 // and change this to a module lint pass
125 MissingDebugImplementations: MissingDebugImplementations::default(),
126 ArrayIntoIter: ArrayIntoIter,
132 macro_rules! late_lint_mod_passes {
133 ($macro:path, $args:tt) => {
137 HardwiredLints: HardwiredLints,
138 ImproperCTypes: ImproperCTypes,
139 VariantSizeDifferences: VariantSizeDifferences,
140 BoxPointers: BoxPointers,
141 PathStatements: PathStatements,
142 // Depends on referenced function signatures in expressions
143 UnusedResults: UnusedResults,
144 NonUpperCaseGlobals: NonUpperCaseGlobals,
145 NonShorthandFieldPatterns: NonShorthandFieldPatterns,
146 UnusedAllocation: UnusedAllocation,
147 // Depends on types used in type definitions
148 MissingCopyImplementations: MissingCopyImplementations,
149 // Depends on referenced function signatures in expressions
150 MutableTransmutes: MutableTransmutes,
151 TypeAliasBounds: TypeAliasBounds,
152 TrivialConstraints: TrivialConstraints,
153 TypeLimits: TypeLimits::new(),
154 NonSnakeCase: NonSnakeCase,
155 InvalidNoMangleItems: InvalidNoMangleItems,
156 // Depends on access levels
157 UnreachablePub: UnreachablePub,
158 ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
159 InvalidValue: InvalidValue,
165 macro_rules! declare_combined_late_pass {
166 ([$v:vis $name:ident], $passes:tt) => (
167 late_lint_methods!(declare_combined_late_lint_pass, [$v $name, $passes], ['tcx]);
171 // FIXME: Make a separate lint type which do not require typeck tables
172 late_lint_passes!(declare_combined_late_pass, [pub BuiltinCombinedLateLintPass]);
174 late_lint_mod_passes!(declare_combined_late_pass, [BuiltinCombinedModuleLateLintPass]);
176 pub fn new_lint_store(no_interleave_lints: bool, internal_lints: bool) -> lint::LintStore {
177 let mut lint_store = lint::LintStore::new();
179 register_builtins(&mut lint_store, no_interleave_lints);
181 register_internals(&mut lint_store);
187 /// Tell the `LintStore` about all the built-in lints (the ones
188 /// defined in this crate and the ones defined in
189 /// `rustc::lint::builtin`).
190 fn register_builtins(store: &mut lint::LintStore, no_interleave_lints: bool) {
191 macro_rules! add_lint_group {
192 ($name:expr, $($lint:ident),*) => (
193 store.register_group(false, $name, None, vec![$(LintId::of($lint)),*]);
197 macro_rules! register_pass {
198 ($method:ident, $ty:ident, $constructor:expr) => {
199 store.register_lints(&$ty::get_lints());
200 store.$method(|| box $constructor);
204 macro_rules! register_passes {
205 ($method:ident, [$($passes:ident: $constructor:expr,)*]) => (
207 register_pass!($method, $passes, $constructor);
212 if no_interleave_lints {
213 pre_expansion_lint_passes!(register_passes, register_pre_expansion_pass);
214 early_lint_passes!(register_passes, register_early_pass);
215 late_lint_passes!(register_passes, register_late_pass);
216 late_lint_mod_passes!(register_passes, register_late_mod_pass);
218 store.register_lints(&BuiltinCombinedPreExpansionLintPass::get_lints());
219 store.register_lints(&BuiltinCombinedEarlyLintPass::get_lints());
220 store.register_lints(&BuiltinCombinedModuleLateLintPass::get_lints());
221 store.register_lints(&BuiltinCombinedLateLintPass::get_lints());
226 NON_CAMEL_CASE_TYPES,
228 NON_UPPER_CASE_GLOBALS
239 UNREACHABLE_PATTERNS,
240 OVERLAPPING_PATTERNS,
248 UNUSED_EXTERN_CRATES,
257 UNUSED_EXTERN_CRATES,
258 ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
259 ELIDED_LIFETIMES_IN_PATHS,
260 EXPLICIT_OUTLIVES_REQUIREMENTS // FIXME(#52665, #47816) not always applicable and not all
261 // macros are ready for this yet.
264 // FIXME macro crates are not up for this yet, too much
265 // breakage is seen if we try to encourage this lint.
266 // MACRO_USE_EXTERN_CRATE
271 INTRA_DOC_LINK_RESOLUTION_FAILURE,
272 MISSING_DOC_CODE_EXAMPLES,
276 // Register renamed and removed lints.
277 store.register_renamed("single_use_lifetime", "single_use_lifetimes");
278 store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
279 store.register_renamed("bare_trait_object", "bare_trait_objects");
280 store.register_renamed("unstable_name_collision", "unstable_name_collisions");
281 store.register_renamed("unused_doc_comment", "unused_doc_comments");
282 store.register_renamed("async_idents", "keyword_idents");
283 store.register_removed("unknown_features", "replaced by an error");
284 store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
285 store.register_removed("negate_unsigned", "cast a signed value instead");
286 store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
287 // Register lint group aliases.
288 store.register_group_alias("nonstandard_style", "bad_style");
289 // This was renamed to `raw_pointer_derive`, which was then removed,
290 // so it is also considered removed.
291 store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
292 store.register_removed("drop_with_repr_extern", "drop flags have been removed");
293 store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
294 store.register_removed("deprecated_attr", "use `deprecated` instead");
295 store.register_removed(
296 "transmute_from_fn_item_types",
297 "always cast functions before transmuting them",
299 store.register_removed(
300 "hr_lifetime_in_assoc_type",
301 "converted into hard error, see https://github.com/rust-lang/rust/issues/33685",
303 store.register_removed(
304 "inaccessible_extern_crate",
305 "converted into hard error, see https://github.com/rust-lang/rust/issues/36886",
307 store.register_removed(
308 "super_or_self_in_global_path",
309 "converted into hard error, see https://github.com/rust-lang/rust/issues/36888",
311 store.register_removed(
312 "overlapping_inherent_impls",
313 "converted into hard error, see https://github.com/rust-lang/rust/issues/36889",
315 store.register_removed(
316 "illegal_floating_point_constant_pattern",
317 "converted into hard error, see https://github.com/rust-lang/rust/issues/36890",
319 store.register_removed(
320 "illegal_struct_or_enum_constant_pattern",
321 "converted into hard error, see https://github.com/rust-lang/rust/issues/36891",
323 store.register_removed(
324 "lifetime_underscore",
325 "converted into hard error, see https://github.com/rust-lang/rust/issues/36892",
327 store.register_removed(
328 "extra_requirement_in_impl",
329 "converted into hard error, see https://github.com/rust-lang/rust/issues/37166",
331 store.register_removed(
333 "converted into hard error, see https://github.com/rust-lang/rust/issues/38260",
335 store.register_removed(
337 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950",
339 store.register_removed(
340 "resolve_trait_on_defaulted_unit",
341 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950",
343 store.register_removed(
344 "private_no_mangle_fns",
345 "no longer a warning, `#[no_mangle]` functions always exported",
347 store.register_removed(
348 "private_no_mangle_statics",
349 "no longer a warning, `#[no_mangle]` statics always exported",
351 store.register_removed("bad_repr", "replaced with a generic attribute input check");
352 store.register_removed(
353 "duplicate_matcher_binding_name",
354 "converted into hard error, see https://github.com/rust-lang/rust/issues/57742",
356 store.register_removed(
357 "incoherent_fundamental_impls",
358 "converted into hard error, see https://github.com/rust-lang/rust/issues/46205",
360 store.register_removed(
361 "legacy_constructor_visibility",
362 "converted into hard error, see https://github.com/rust-lang/rust/issues/39207",
364 store.register_removed(
365 "legacy_directory_ownership",
366 "converted into hard error, see https://github.com/rust-lang/rust/issues/37872",
368 store.register_removed(
369 "safe_extern_statics",
370 "converted into hard error, see https://github.com/rust-lang/rust/issues/36247",
372 store.register_removed(
373 "parenthesized_params_in_types_and_modules",
374 "converted into hard error, see https://github.com/rust-lang/rust/issues/42238",
376 store.register_removed(
377 "duplicate_macro_exports",
378 "converted into hard error, see https://github.com/rust-lang/rust/issues/35896",
380 store.register_removed(
382 "converted into hard error, see https://github.com/rust-lang/rust/issues/59014",
384 store.register_removed("plugin_as_library", "plugins have been deprecated and retired");
387 fn register_internals(store: &mut lint::LintStore) {
388 store.register_lints(&DefaultHashTypes::get_lints());
389 store.register_early_pass(|| box DefaultHashTypes::new());
390 store.register_lints(&LintPassImpl::get_lints());
391 store.register_early_pass(|| box LintPassImpl);
392 store.register_lints(&TyTyKind::get_lints());
393 store.register_late_pass(|| box TyTyKind);
394 store.register_group(
399 LintId::of(DEFAULT_HASH_TYPES),
400 LintId::of(USAGE_OF_TY_TYKIND),
401 LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
402 LintId::of(TY_PASS_BY_REFERENCE),
403 LintId::of(USAGE_OF_QUALIFIED_TY),