]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/lib.rs
a03f12c3dfbca09dd054c2f732c4a6cda7bcd5f9
[rust.git] / src / librustc_lint / lib.rs
1 // Copyright 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 //! Lints in the Rust compiler.
12 //!
13 //! This currently only contains the definitions and implementations
14 //! of most of the lints that `rustc` supports directly, it does not
15 //! contain the infrastructure for defining/registering lints. That is
16 //! available in `rustc::lint` and `rustc_plugin` respectively.
17 //!
18 //! # Note
19 //!
20 //! This API is completely unstable and subject to change.
21
22 #![crate_name = "rustc_lint"]
23 #![crate_type = "dylib"]
24 #![crate_type = "rlib"]
25 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
26       html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
27       html_root_url = "https://doc.rust-lang.org/nightly/")]
28 #![deny(warnings)]
29
30 #![cfg_attr(test, feature(test))]
31 #![feature(box_patterns)]
32 #![feature(box_syntax)]
33 #![feature(i128_type)]
34 #![feature(quote)]
35 #![feature(rustc_diagnostic_macros)]
36 #![feature(slice_patterns)]
37
38 #[macro_use]
39 extern crate syntax;
40 #[macro_use]
41 extern crate rustc;
42 #[macro_use]
43 extern crate log;
44 extern crate rustc_back;
45 extern crate rustc_const_eval;
46 extern crate syntax_pos;
47
48 pub use rustc::lint;
49 pub use rustc::middle;
50 pub use rustc::session;
51 pub use rustc::util;
52
53 use session::Session;
54 use lint::LintId;
55 use lint::FutureIncompatibleInfo;
56
57 mod bad_style;
58 mod builtin;
59 mod types;
60 mod unused;
61
62 use bad_style::*;
63 use builtin::*;
64 use types::*;
65 use unused::*;
66
67 /// Tell the `LintStore` about all the built-in lints (the ones
68 /// defined in this crate and the ones defined in
69 /// `rustc::lint::builtin`).
70 pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
71     macro_rules! add_builtin {
72         ($sess:ident, $($name:ident),*,) => (
73             {$(
74                 store.register_late_pass($sess, false, box $name);
75                 )*}
76             )
77     }
78
79     macro_rules! add_early_builtin {
80         ($sess:ident, $($name:ident),*,) => (
81             {$(
82                 store.register_early_pass($sess, false, box $name);
83                 )*}
84             )
85     }
86
87     macro_rules! add_builtin_with_new {
88         ($sess:ident, $($name:ident),*,) => (
89             {$(
90                 store.register_late_pass($sess, false, box $name::new());
91                 )*}
92             )
93     }
94
95     macro_rules! add_early_builtin_with_new {
96         ($sess:ident, $($name:ident),*,) => (
97             {$(
98                 store.register_early_pass($sess, false, box $name::new());
99                 )*}
100             )
101     }
102
103     macro_rules! add_lint_group {
104         ($sess:ident, $name:expr, $($lint:ident),*) => (
105             store.register_group($sess, false, $name, vec![$(LintId::of($lint)),*]);
106             )
107     }
108
109     add_early_builtin!(sess,
110                        UnusedParens,
111                        UnusedImportBraces,
112                        AnonymousParameters,
113                        IllegalFloatLiteralPattern,
114                        );
115
116     add_early_builtin_with_new!(sess,
117                                 DeprecatedAttr,
118                                 );
119
120     add_builtin!(sess,
121                  HardwiredLints,
122                  WhileTrue,
123                  ImproperCTypes,
124                  VariantSizeDifferences,
125                  BoxPointers,
126                  UnusedAttributes,
127                  PathStatements,
128                  UnusedResults,
129                  NonCamelCaseTypes,
130                  NonSnakeCase,
131                  NonUpperCaseGlobals,
132                  NonShorthandFieldPatterns,
133                  UnusedUnsafe,
134                  UnsafeCode,
135                  UnusedMut,
136                  UnusedAllocation,
137                  MissingCopyImplementations,
138                  UnstableFeatures,
139                  UnconditionalRecursion,
140                  InvalidNoMangleItems,
141                  PluginAsLibrary,
142                  MutableTransmutes,
143                  UnionsWithDropFields,
144                  );
145
146     add_builtin_with_new!(sess,
147                           TypeLimits,
148                           MissingDoc,
149                           MissingDebugImplementations,
150                           );
151
152     add_lint_group!(sess,
153                     "bad_style",
154                     NON_CAMEL_CASE_TYPES,
155                     NON_SNAKE_CASE,
156                     NON_UPPER_CASE_GLOBALS);
157
158     add_lint_group!(sess,
159                     "unused",
160                     UNUSED_IMPORTS,
161                     UNUSED_VARIABLES,
162                     UNUSED_ASSIGNMENTS,
163                     DEAD_CODE,
164                     UNUSED_MUT,
165                     UNREACHABLE_CODE,
166                     UNREACHABLE_PATTERNS,
167                     UNUSED_MUST_USE,
168                     UNUSED_UNSAFE,
169                     PATH_STATEMENTS,
170                     UNUSED_ATTRIBUTES,
171                     UNUSED_MACROS);
172
173     // Guidelines for creating a future incompatibility lint:
174     //
175     // - Create a lint defaulting to warn as normal, with ideally the same error
176     //   message you would normally give
177     // - Add a suitable reference, typically an RFC or tracking issue. Go ahead
178     //   and include the full URL, sort items in ascending order of issue numbers.
179     // - Later, change lint to error
180     // - Eventually, remove lint
181     store.register_future_incompatible(sess,
182                                        vec![
183         FutureIncompatibleInfo {
184             id: LintId::of(PRIVATE_IN_PUBLIC),
185             reference: "issue #34537 <https://github.com/rust-lang/rust/issues/34537>",
186         },
187         FutureIncompatibleInfo {
188             id: LintId::of(PUB_USE_OF_PRIVATE_EXTERN_CRATE),
189             reference: "issue #34537 <https://github.com/rust-lang/rust/issues/34537>",
190         },
191         FutureIncompatibleInfo {
192             id: LintId::of(PATTERNS_IN_FNS_WITHOUT_BODY),
193             reference: "issue #35203 <https://github.com/rust-lang/rust/issues/35203>",
194         },
195         FutureIncompatibleInfo {
196             id: LintId::of(SAFE_EXTERN_STATICS),
197             reference: "issue #36247 <https://github.com/rust-lang/rust/issues/36247>",
198         },
199         FutureIncompatibleInfo {
200             id: LintId::of(INVALID_TYPE_PARAM_DEFAULT),
201             reference: "issue #36887 <https://github.com/rust-lang/rust/issues/36887>",
202         },
203         FutureIncompatibleInfo {
204             id: LintId::of(EXTRA_REQUIREMENT_IN_IMPL),
205             reference: "issue #37166 <https://github.com/rust-lang/rust/issues/37166>",
206         },
207         FutureIncompatibleInfo {
208             id: LintId::of(LEGACY_DIRECTORY_OWNERSHIP),
209             reference: "issue #37872 <https://github.com/rust-lang/rust/issues/37872>",
210         },
211         FutureIncompatibleInfo {
212             id: LintId::of(LEGACY_IMPORTS),
213             reference: "issue #38260 <https://github.com/rust-lang/rust/issues/38260>",
214         },
215         FutureIncompatibleInfo {
216             id: LintId::of(LEGACY_CONSTRUCTOR_VISIBILITY),
217             reference: "issue #39207 <https://github.com/rust-lang/rust/issues/39207>",
218         },
219         FutureIncompatibleInfo {
220             id: LintId::of(RESOLVE_TRAIT_ON_DEFAULTED_UNIT),
221             reference: "issue #39216 <https://github.com/rust-lang/rust/issues/39216>",
222         },
223         FutureIncompatibleInfo {
224             id: LintId::of(MISSING_FRAGMENT_SPECIFIER),
225             reference: "issue #40107 <https://github.com/rust-lang/rust/issues/40107>",
226         },
227         FutureIncompatibleInfo {
228             id: LintId::of(ILLEGAL_FLOATING_POINT_LITERAL_PATTERN),
229             reference: "issue #41620 <https://github.com/rust-lang/rust/issues/41620>",
230         },
231         FutureIncompatibleInfo {
232             id: LintId::of(ANONYMOUS_PARAMETERS),
233             reference: "issue #41686 <https://github.com/rust-lang/rust/issues/41686>",
234         },
235         FutureIncompatibleInfo {
236             id: LintId::of(PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES),
237             reference: "issue #42238 <https://github.com/rust-lang/rust/issues/42238>",
238         }
239         ]);
240
241     // Register renamed and removed lints
242     store.register_renamed("unknown_features", "unused_features");
243     store.register_removed("unsigned_negation",
244                            "replaced by negate_unsigned feature gate");
245     store.register_removed("negate_unsigned", "cast a signed value instead");
246     store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
247     // This was renamed to raw_pointer_derive, which was then removed,
248     // so it is also considered removed
249     store.register_removed("raw_pointer_deriving",
250                            "using derive with raw pointers is ok");
251     store.register_removed("drop_with_repr_extern", "drop flags have been removed");
252     store.register_removed("transmute_from_fn_item_types",
253         "always cast functions before transmuting them");
254     store.register_removed("hr_lifetime_in_assoc_type",
255         "converted into hard error, see https://github.com/rust-lang/rust/issues/33685");
256     store.register_removed("inaccessible_extern_crate",
257         "converted into hard error, see https://github.com/rust-lang/rust/issues/36886");
258     store.register_removed("super_or_self_in_global_path",
259         "converted into hard error, see https://github.com/rust-lang/rust/issues/36888");
260     store.register_removed("overlapping_inherent_impls",
261         "converted into hard error, see https://github.com/rust-lang/rust/issues/36889");
262     store.register_removed("illegal_floating_point_constant_pattern",
263         "converted into hard error, see https://github.com/rust-lang/rust/issues/36890");
264     store.register_removed("illegal_struct_or_enum_constant_pattern",
265         "converted into hard error, see https://github.com/rust-lang/rust/issues/36891");
266     store.register_removed("lifetime_underscore",
267         "converted into hard error, see https://github.com/rust-lang/rust/issues/36892");
268 }