]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/lib.rs
Rollup merge of #34403 - jonathandturner:move_liberror, r=alexcrichton
[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 #![unstable(feature = "rustc_private", issue = "27812")]
24 #![crate_type = "dylib"]
25 #![crate_type = "rlib"]
26 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
27       html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
28       html_root_url = "https://doc.rust-lang.org/nightly/")]
29 #![cfg_attr(not(stage0), deny(warnings))]
30
31 #![cfg_attr(test, feature(test))]
32 #![feature(box_patterns)]
33 #![feature(box_syntax)]
34 #![feature(quote)]
35 #![feature(rustc_diagnostic_macros)]
36 #![feature(rustc_private)]
37 #![feature(slice_patterns)]
38 #![feature(staged_api)]
39
40 #[macro_use]
41 extern crate syntax;
42 #[macro_use]
43 extern crate rustc;
44 #[macro_use]
45 extern crate log;
46 extern crate rustc_back;
47 extern crate rustc_const_eval;
48 extern crate syntax_pos;
49
50 pub use rustc::lint as lint;
51 pub use rustc::middle as middle;
52 pub use rustc::session as session;
53 pub use rustc::util as util;
54
55 use session::Session;
56 use lint::LintId;
57 use lint::FutureIncompatibleInfo;
58
59 mod bad_style;
60 mod builtin;
61 mod types;
62 mod unused;
63
64 use bad_style::*;
65 use builtin::*;
66 use types::*;
67 use unused::*;
68
69 /// Tell the `LintStore` about all the built-in lints (the ones
70 /// defined in this crate and the ones defined in
71 /// `rustc::lint::builtin`).
72 pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
73     macro_rules! add_builtin {
74         ($sess:ident, $($name:ident),*,) => (
75             {$(
76                 store.register_late_pass($sess, false, box $name);
77                 )*}
78             )
79     }
80
81     macro_rules! add_early_builtin {
82         ($sess:ident, $($name:ident),*,) => (
83             {$(
84                 store.register_early_pass($sess, false, box $name);
85                 )*}
86             )
87     }
88
89     macro_rules! add_builtin_with_new {
90         ($sess:ident, $($name:ident),*,) => (
91             {$(
92                 store.register_late_pass($sess, false, box $name::new());
93                 )*}
94             )
95     }
96
97     macro_rules! add_lint_group {
98         ($sess:ident, $name:expr, $($lint:ident),*) => (
99             store.register_group($sess, false, $name, vec![$(LintId::of($lint)),*]);
100             )
101     }
102
103     add_early_builtin!(sess,
104                        UnusedParens,
105                        );
106
107     add_builtin!(sess,
108                  HardwiredLints,
109                  WhileTrue,
110                  ImproperCTypes,
111                  BoxPointers,
112                  UnusedAttributes,
113                  PathStatements,
114                  UnusedResults,
115                  NonCamelCaseTypes,
116                  NonSnakeCase,
117                  NonUpperCaseGlobals,
118                  UnusedImportBraces,
119                  NonShorthandFieldPatterns,
120                  UnusedUnsafe,
121                  UnsafeCode,
122                  UnusedMut,
123                  UnusedAllocation,
124                  MissingCopyImplementations,
125                  UnstableFeatures,
126                  Deprecated,
127                  UnconditionalRecursion,
128                  InvalidNoMangleItems,
129                  PluginAsLibrary,
130                  DropWithReprExtern,
131                  MutableTransmutes,
132                  );
133
134     add_builtin_with_new!(sess,
135                           TypeLimits,
136                           MissingDoc,
137                           MissingDebugImplementations,
138                           );
139
140     add_lint_group!(sess, "bad_style",
141                     NON_CAMEL_CASE_TYPES, NON_SNAKE_CASE, NON_UPPER_CASE_GLOBALS);
142
143     add_lint_group!(sess, "unused",
144                     UNUSED_IMPORTS, UNUSED_VARIABLES, UNUSED_ASSIGNMENTS, DEAD_CODE,
145                     UNUSED_MUT, UNREACHABLE_CODE, UNUSED_MUST_USE,
146                     UNUSED_UNSAFE, PATH_STATEMENTS, UNUSED_ATTRIBUTES);
147
148     // Guidelines for creating a future incompatibility lint:
149     //
150     // - Create a lint defaulting to warn as normal, with ideally the same error
151     //   message you would normally give
152     // - Add a suitable reference, typically an RFC or tracking issue. Go ahead
153     //   and include the full URL.
154     // - Later, change lint to error
155     // - Eventually, remove lint
156     store.register_future_incompatible(sess, vec![
157         FutureIncompatibleInfo {
158             id: LintId::of(PRIVATE_IN_PUBLIC),
159             reference: "the explanation for E0446 (`--explain E0446`)",
160         },
161         FutureIncompatibleInfo {
162             id: LintId::of(INACCESSIBLE_EXTERN_CRATE),
163             reference: "PR 31362 <https://github.com/rust-lang/rust/pull/31362>",
164         },
165         FutureIncompatibleInfo {
166             id: LintId::of(INVALID_TYPE_PARAM_DEFAULT),
167             reference: "PR 30724 <https://github.com/rust-lang/rust/pull/30724>",
168         },
169         FutureIncompatibleInfo {
170             id: LintId::of(SUPER_OR_SELF_IN_GLOBAL_PATH),
171             reference: "PR #32403 <https://github.com/rust-lang/rust/pull/32403>",
172         },
173         FutureIncompatibleInfo {
174             id: LintId::of(MATCH_OF_UNIT_VARIANT_VIA_PAREN_DOTDOT),
175             reference: "RFC 218 <https://github.com/rust-lang/rfcs/blob/\
176                         master/text/0218-empty-struct-with-braces.md>",
177         },
178         FutureIncompatibleInfo {
179             id: LintId::of(TRANSMUTE_FROM_FN_ITEM_TYPES),
180             reference: "issue #19925 <https://github.com/rust-lang/rust/issues/19925>",
181         },
182         FutureIncompatibleInfo {
183             id: LintId::of(OVERLAPPING_INHERENT_IMPLS),
184             reference: "issue #22889 <https://github.com/rust-lang/rust/issues/22889>",
185         },
186         FutureIncompatibleInfo {
187             id: LintId::of(ILLEGAL_FLOATING_POINT_CONSTANT_PATTERN),
188             reference: "RFC 1445 <https://github.com/rust-lang/rfcs/pull/1445>",
189         },
190         FutureIncompatibleInfo {
191             id: LintId::of(ILLEGAL_STRUCT_OR_ENUM_CONSTANT_PATTERN),
192             reference: "RFC 1445 <https://github.com/rust-lang/rfcs/pull/1445>",
193         },
194         FutureIncompatibleInfo {
195             id: LintId::of(UNSIZED_IN_TUPLE),
196             reference: "issue #33242 <https://github.com/rust-lang/rust/issues/33242>",
197         },
198         FutureIncompatibleInfo {
199             id: LintId::of(OBJECT_UNSAFE_FRAGMENT),
200             reference: "issue #33243 <https://github.com/rust-lang/rust/issues/33243>",
201         },
202         FutureIncompatibleInfo {
203             id: LintId::of(HR_LIFETIME_IN_ASSOC_TYPE),
204             reference: "issue #33685 <https://github.com/rust-lang/rust/issues/33685>",
205         },
206         FutureIncompatibleInfo {
207             id: LintId::of(LIFETIME_UNDERSCORE),
208             reference: "RFC 1177 <https://github.com/rust-lang/rfcs/pull/1177>",
209         },
210         ]);
211
212     // We have one lint pass defined specially
213     store.register_late_pass(sess, false, box lint::GatherNodeLevels);
214
215     // Register renamed and removed lints
216     store.register_renamed("unknown_features", "unused_features");
217     store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
218     store.register_removed("negate_unsigned", "cast a signed value instead");
219     store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
220     // This was renamed to raw_pointer_derive, which was then removed,
221     // so it is also considered removed
222     store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
223 }