]> git.lizzy.rs Git - rust.git/blob - src/doc/rustc/src/lints/listing/deny-by-default.md
document deny by default lints
[rust.git] / src / doc / rustc / src / lints / listing / deny-by-default.md
1 # Deny-by-default lints
2
3 These lints are all set to the 'deny' level by default.
4
5 ## exceeding-bitshifts
6
7 This lint detects that a shift exceeds the type's number of bits. Some
8 example code that triggers this lint:
9
10 ```rust
11 1_i32 << 32;
12 ```
13
14 This will produce:
15
16 ```text
17 error: bitshift exceeds the type's number of bits
18  --> src/main.rs:2:5
19   |
20 2 |     1_i32 << 32;
21   |     ^^^^^^^^^^^
22   |
23 ```
24
25 ## invalid-type-param-default
26
27 This lint detects type parameter default erroneously allowed in invalid location. Some
28 example code that triggers this lint:
29
30 ```rust
31 fn foo<T=i32>(t: T) {}
32 ```
33
34 This will produce:
35
36 ```text
37 error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions.
38  --> src/main.rs:4:8
39   |
40 4 | fn foo<T=i32>(t: T) {}
41   |        ^
42   |
43   = note: #[deny(invalid_type_param_default)] on by default
44   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
45   = note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>
46 ```
47
48 ## legacy-constructor-visibility
49
50 [RFC 1506](https://github.com/rust-lang/rfcs/blob/master/text/1506-adt-kinds.md) modified some
51 visibility rules, and changed the visibility of struct constructors. Some
52 example code that triggers this lint:
53
54 ```rust
55 mod m {
56     pub struct S(u8);
57     
58     fn f() {
59         // this is trying to use S from the 'use' line, but becuase the `u8` is
60         // not pub, it is private
61         ::S;
62     }
63 }
64
65 use m::S;
66 ```
67
68 This will produce:
69
70 ```text
71 error: private struct constructors are not usable through re-exports in outer modules
72  --> src/main.rs:5:9
73   |
74 5 |         ::S;
75   |         ^^^
76   |
77   = note: #[deny(legacy_constructor_visibility)] on by default
78   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
79   = note: for more information, see issue #39207 <https://github.com/rust-lang/rust/issues/39207>
80 ```
81
82
83 ## legacy-directory-ownership
84
85 The legacy_directory_ownership warning is issued when
86
87 * There is a non-inline module with a #[path] attribute (e.g. #[path = "foo.rs"] mod bar;),
88 * The module's file ("foo.rs" in the above example) is not named "mod.rs", and
89 * The module's file contains a non-inline child module without a #[path] attribute.
90
91 The warning can be fixed by renaming the parent module to "mod.rs" and moving
92 it into its own directory if appropriate.
93
94 ## legacy-imports
95
96 This lint detects names that resolve to ambiguous glob imports. Some example
97 code that triggers this lint:
98
99 ```rust
100 pub struct Foo;
101
102 mod bar {
103     struct Foo;
104
105     mod baz {
106         use *;
107         use bar::*;
108         fn f(_: Foo) {}
109     }
110 }
111 ```
112
113 This will produce:
114
115 ```text
116 error: `Foo` is ambiguous
117  --> src/main.rs:9:17
118   |
119 7 |         use *;
120   |             - `Foo` could refer to the name imported here
121 8 |         use bar::*;
122   |             ------ `Foo` could also refer to the name imported here
123 9 |         fn f(_: Foo) {}
124   |                 ^^^
125   |
126   = note: #[deny(legacy_imports)] on by default
127   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
128   = note: for more information, see issue #38260 <https://github.com/rust-lang/rust/issues/38260>
129 ```
130
131
132 ## missing-fragment-specifier
133
134 The missing_fragment_specifier warning is issued when an unused pattern in a
135 `macro_rules!` macro definition has a meta-variable (e.g. `$e`) that is not
136 followed by a fragment specifier (e.g. `:expr`).
137
138 This warning can always be fixed by removing the unused pattern in the
139 `macro_rules!` macro definition.
140
141 ## mutable-transmutes
142
143 This lint catches transmuting from `&T` to `&mut T` becuase it is undefined
144 behavior. Some example code that triggers this lint:
145
146 ```rust
147 unsafe {
148     let y = std::mem::transmute::<&i32, &mut i32>(&5);
149 }
150 ```
151
152 This will produce:
153
154 ```text
155 error: mutating transmuted &mut T from &T may cause undefined behavior, consider instead using an UnsafeCell
156  --> src/main.rs:3:17
157   |
158 3 |         let y = std::mem::transmute::<&i32, &mut i32>(&5);
159   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
160   |
161 ```
162
163
164 ## no-mangle-const-items
165
166 This lint detects any `const` items with the `#[no_mangle]` attribute.
167 Constants do not have their symbols exported, and therefore, this probably
168 means you meant to use a `static`, not a `const. Some example code that
169 triggers this lint:
170
171 ```rust
172 #[no_mangle]
173 const FOO: i32 = 5;
174 ```
175
176 This will produce:
177
178 ```text
179 error: const items should never be #[no_mangle]
180  --> src/main.rs:3:1
181   |
182 3 | const FOO: i32 = 5;
183   | -----^^^^^^^^^^^^^^
184   | |
185   | help: try a static value: `pub static`
186   |
187 ```
188
189 ## parenthesized-params-in-types-and-modules
190
191 This lint detects incorrect parentheses. Some example code that triggers this
192 lint:
193
194 ```rust
195 let x = 5 as usize();
196 ```
197
198 This will produce:
199
200 ```text
201 error: parenthesized parameters may only be used with a trait
202  --> src/main.rs:2:21
203   |
204 2 |   let x = 5 as usize();
205   |                     ^^
206   |
207   = note: #[deny(parenthesized_params_in_types_and_modules)] on by default
208   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
209   = note: for more information, see issue #42238 <https://github.com/rust-lang/rust/issues/42238>
210 ```
211
212 To fix it, remove the `()`s.
213
214 ## pub-use-of-private-extern-crate
215
216 This lint detects a specific situation of re-exporting a private `extern crate`;
217
218 ## safe-extern-statics
219
220 In older versions of Rust, there was a soundness issue where `extern static`s were allowed
221 to be accessed in safe code. This lint now catches and denies this kind of code.
222
223 ## unknown-crate-types
224
225 This lint detects an unknown crate type found in a `#[crate_type]` directive. Some
226 example code that triggers this lint:
227
228 ```rust
229 #![crate_type="lol"]
230 ```
231
232 This will produce:
233
234 ```text
235 error: invalid `crate_type` value
236  --> src/lib.rs:1:1
237   |
238 1 | #![crate_type="lol"]
239   | ^^^^^^^^^^^^^^^^^^^^
240   |
241 ```