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/")]
14 #![cfg_attr(test, feature(test))]
15 #![feature(box_patterns)]
16 #![feature(box_syntax)]
18 #![feature(matches_macro)]
20 #![recursion_limit="256"]
26 mod nonstandard_style;
27 mod redundant_semicolon;
34 use rustc::lint::{EarlyContext, LateContext, LateLintPass, EarlyLintPass, LintPass, LintArray};
35 use rustc::lint::builtin::{
37 ELIDED_LIFETIMES_IN_PATHS,
38 EXPLICIT_OUTLIVES_REQUIREMENTS,
39 INTRA_DOC_LINK_RESOLUTION_FAILURE,
40 MISSING_DOC_CODE_EXAMPLES,
44 use rustc::hir::def_id::DefId;
45 use rustc::ty::query::Providers;
46 use rustc::ty::TyCtxt;
53 use redundant_semicolon::*;
54 use nonstandard_style::*;
58 use non_ascii_idents::*;
59 use rustc::lint::internal::*;
61 /// Useful for other parts of the compiler.
62 pub use builtin::SoftLints;
64 pub fn provide(providers: &mut Providers<'_>) {
65 *providers = Providers {
71 fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) {
72 lint::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
75 macro_rules! pre_expansion_lint_passes {
76 ($macro:path, $args:tt) => (
78 KeywordIdents: KeywordIdents,
79 UnusedDocComment: UnusedDocComment,
84 macro_rules! early_lint_passes {
85 ($macro:path, $args:tt) => (
87 UnusedParens: UnusedParens,
88 UnusedImportBraces: UnusedImportBraces,
89 UnsafeCode: UnsafeCode,
90 AnonymousParameters: AnonymousParameters,
91 EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
92 NonCamelCaseTypes: NonCamelCaseTypes,
93 DeprecatedAttr: DeprecatedAttr::new(),
95 NonAsciiIdents: NonAsciiIdents,
96 IncompleteFeatures: IncompleteFeatures,
97 RedundantSemicolon: RedundantSemicolon,
102 macro_rules! declare_combined_early_pass {
103 ([$name:ident], $passes:tt) => (
104 early_lint_methods!(declare_combined_early_lint_pass, [pub $name, $passes]);
108 pre_expansion_lint_passes!(declare_combined_early_pass, [BuiltinCombinedPreExpansionLintPass]);
109 early_lint_passes!(declare_combined_early_pass, [BuiltinCombinedEarlyLintPass]);
111 macro_rules! late_lint_passes {
112 ($macro:path, $args:tt) => (
114 // FIXME: Look into regression when this is used as a module lint
115 // May Depend on constants elsewhere
116 UnusedBrokenConst: UnusedBrokenConst,
118 // Uses attr::is_used which is untracked, can't be an incremental module pass.
119 UnusedAttributes: UnusedAttributes::new(),
121 // Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
122 UnstableFeatures: UnstableFeatures,
124 // Tracks state across modules
125 UnnameableTestItems: UnnameableTestItems::new(),
127 // Tracks attributes of parents
128 MissingDoc: MissingDoc::new(),
130 // Depends on access levels
131 // FIXME: Turn the computation of types which implement Debug into a query
132 // and change this to a module lint pass
133 MissingDebugImplementations: MissingDebugImplementations::default(),
138 macro_rules! late_lint_mod_passes {
139 ($macro:path, $args:tt) => (
141 HardwiredLints: HardwiredLints,
142 ImproperCTypes: ImproperCTypes,
143 VariantSizeDifferences: VariantSizeDifferences,
144 BoxPointers: BoxPointers,
145 PathStatements: PathStatements,
147 // Depends on referenced function signatures in expressions
148 UnusedResults: UnusedResults,
150 NonUpperCaseGlobals: NonUpperCaseGlobals,
151 NonShorthandFieldPatterns: NonShorthandFieldPatterns,
152 UnusedAllocation: UnusedAllocation,
154 // Depends on types used in type definitions
155 MissingCopyImplementations: MissingCopyImplementations,
157 PluginAsLibrary: PluginAsLibrary,
159 // Depends on referenced function signatures in expressions
160 MutableTransmutes: MutableTransmutes,
162 TypeAliasBounds: TypeAliasBounds,
164 TrivialConstraints: TrivialConstraints,
165 TypeLimits: TypeLimits::new(),
167 NonSnakeCase: NonSnakeCase,
168 InvalidNoMangleItems: InvalidNoMangleItems,
170 // Depends on access levels
171 UnreachablePub: UnreachablePub,
173 ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
174 InvalidValue: InvalidValue,
179 macro_rules! declare_combined_late_pass {
180 ([$v:vis $name:ident], $passes:tt) => (
181 late_lint_methods!(declare_combined_late_lint_pass, [$v $name, $passes], ['tcx]);
185 // FIXME: Make a separate lint type which do not require typeck tables
186 late_lint_passes!(declare_combined_late_pass, [pub BuiltinCombinedLateLintPass]);
188 late_lint_mod_passes!(declare_combined_late_pass, [BuiltinCombinedModuleLateLintPass]);
190 pub fn new_lint_store(no_interleave_lints: bool, internal_lints: bool) -> lint::LintStore {
191 let mut lint_store = lint::LintStore::new();
193 register_builtins(&mut lint_store, no_interleave_lints);
195 register_internals(&mut lint_store);
201 /// Tell the `LintStore` about all the built-in lints (the ones
202 /// defined in this crate and the ones defined in
203 /// `rustc::lint::builtin`).
204 fn register_builtins(store: &mut lint::LintStore, no_interleave_lints: bool) {
205 macro_rules! add_lint_group {
206 ($name:expr, $($lint:ident),*) => (
207 store.register_group(false, $name, None, vec![$(LintId::of($lint)),*]);
211 macro_rules! register_pass {
212 ($method:ident, $ty:ident, $constructor:expr) => (
213 store.register_lints(&$ty::get_lints());
214 store.$method(|| box $constructor);
218 macro_rules! register_passes {
219 ($method:ident, [$($passes:ident: $constructor:expr,)*]) => (
221 register_pass!($method, $passes, $constructor);
226 if no_interleave_lints {
227 pre_expansion_lint_passes!(register_passes, register_pre_expansion_pass);
228 early_lint_passes!(register_passes, register_early_pass);
229 late_lint_passes!(register_passes, register_late_pass);
230 late_lint_mod_passes!(register_passes, register_late_mod_pass);
232 store.register_lints(&BuiltinCombinedPreExpansionLintPass::get_lints());
233 store.register_lints(&BuiltinCombinedEarlyLintPass::get_lints());
234 store.register_lints(&BuiltinCombinedModuleLateLintPass::get_lints());
235 store.register_lints(&BuiltinCombinedLateLintPass::get_lints());
238 add_lint_group!("nonstandard_style",
239 NON_CAMEL_CASE_TYPES,
241 NON_UPPER_CASE_GLOBALS);
243 add_lint_group!("unused",
250 UNREACHABLE_PATTERNS,
251 OVERLAPPING_PATTERNS,
259 UNUSED_EXTERN_CRATES,
264 add_lint_group!("rust_2018_idioms",
266 UNUSED_EXTERN_CRATES,
267 ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
268 ELIDED_LIFETIMES_IN_PATHS,
269 EXPLICIT_OUTLIVES_REQUIREMENTS
271 // FIXME(#52665, #47816) not always applicable and not all
272 // macros are ready for this yet.
275 // FIXME macro crates are not up for this yet, too much
276 // breakage is seen if we try to encourage this lint.
277 // MACRO_USE_EXTERN_CRATE,
280 add_lint_group!("rustdoc",
281 INTRA_DOC_LINK_RESOLUTION_FAILURE,
282 MISSING_DOC_CODE_EXAMPLES,
285 // Register renamed and removed lints.
286 store.register_renamed("single_use_lifetime", "single_use_lifetimes");
287 store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
288 store.register_renamed("bare_trait_object", "bare_trait_objects");
289 store.register_renamed("unstable_name_collision", "unstable_name_collisions");
290 store.register_renamed("unused_doc_comment", "unused_doc_comments");
291 store.register_renamed("async_idents", "keyword_idents");
292 store.register_removed("unknown_features", "replaced by an error");
293 store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
294 store.register_removed("negate_unsigned", "cast a signed value instead");
295 store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
296 // Register lint group aliases.
297 store.register_group_alias("nonstandard_style", "bad_style");
298 // This was renamed to `raw_pointer_derive`, which was then removed,
299 // so it is also considered removed.
300 store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
301 store.register_removed("drop_with_repr_extern", "drop flags have been removed");
302 store.register_removed("fat_ptr_transmutes", "was accidentally removed back in 2014");
303 store.register_removed("deprecated_attr", "use `deprecated` instead");
304 store.register_removed("transmute_from_fn_item_types",
305 "always cast functions before transmuting them");
306 store.register_removed("hr_lifetime_in_assoc_type",
307 "converted into hard error, see https://github.com/rust-lang/rust/issues/33685");
308 store.register_removed("inaccessible_extern_crate",
309 "converted into hard error, see https://github.com/rust-lang/rust/issues/36886");
310 store.register_removed("super_or_self_in_global_path",
311 "converted into hard error, see https://github.com/rust-lang/rust/issues/36888");
312 store.register_removed("overlapping_inherent_impls",
313 "converted into hard error, see https://github.com/rust-lang/rust/issues/36889");
314 store.register_removed("illegal_floating_point_constant_pattern",
315 "converted into hard error, see https://github.com/rust-lang/rust/issues/36890");
316 store.register_removed("illegal_struct_or_enum_constant_pattern",
317 "converted into hard error, see https://github.com/rust-lang/rust/issues/36891");
318 store.register_removed("lifetime_underscore",
319 "converted into hard error, see https://github.com/rust-lang/rust/issues/36892");
320 store.register_removed("extra_requirement_in_impl",
321 "converted into hard error, see https://github.com/rust-lang/rust/issues/37166");
322 store.register_removed("legacy_imports",
323 "converted into hard error, see https://github.com/rust-lang/rust/issues/38260");
324 store.register_removed("coerce_never",
325 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
326 store.register_removed("resolve_trait_on_defaulted_unit",
327 "converted into hard error, see https://github.com/rust-lang/rust/issues/48950");
328 store.register_removed("private_no_mangle_fns",
329 "no longer a warning, `#[no_mangle]` functions always exported");
330 store.register_removed("private_no_mangle_statics",
331 "no longer a warning, `#[no_mangle]` statics always exported");
332 store.register_removed("bad_repr",
333 "replaced with a generic attribute input check");
334 store.register_removed("duplicate_matcher_binding_name",
335 "converted into hard error, see https://github.com/rust-lang/rust/issues/57742");
336 store.register_removed("incoherent_fundamental_impls",
337 "converted into hard error, see https://github.com/rust-lang/rust/issues/46205");
340 fn register_internals(store: &mut lint::LintStore) {
341 store.register_lints(&DefaultHashTypes::get_lints());
342 store.register_early_pass(|| box DefaultHashTypes::new());
343 store.register_lints(&LintPassImpl::get_lints());
344 store.register_early_pass(|| box LintPassImpl);
345 store.register_lints(&TyTyKind::get_lints());
346 store.register_late_pass(|| box TyTyKind);
347 store.register_group(
352 LintId::of(DEFAULT_HASH_TYPES),
353 LintId::of(USAGE_OF_TY_TYKIND),
354 LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
355 LintId::of(TY_PASS_BY_REFERENCE),
356 LintId::of(USAGE_OF_QUALIFIED_TY),