]> git.lizzy.rs Git - rust.git/blob - src/doc/rustc/src/lints/listing/warn-by-default.md
Fixed type-alias-bounds lint doc
[rust.git] / src / doc / rustc / src / lints / listing / warn-by-default.md
1 # Warn-by-default lints
2
3 These lints are all set to the 'warn' level by default.
4
5 ## const-err
6
7 This lint detects an erroneous expression while doing constant evaluation. Some
8 example code that triggers this lint:
9
10 ```rust,ignore
11 let b = 200u8 + 200u8;
12 ```
13
14 This will produce:
15
16 ```text
17 warning: attempt to add with overflow
18  --> src/main.rs:2:9
19   |
20 2 | let b = 200u8 + 200u8;
21   |         ^^^^^^^^^^^^^
22   |
23 ```
24
25 ## dead-code
26
27 This lint detects unused, unexported items. Some
28 example code that triggers this lint:
29
30 ```rust
31 fn foo() {}
32 ```
33
34 This will produce:
35
36 ```text
37 warning: function is never used: `foo`
38  --> src/lib.rs:2:1
39   |
40 2 | fn foo() {}
41   | ^^^^^^^^
42   |
43 ```
44
45 ## deprecated
46
47 This lint detects use of deprecated items. Some
48 example code that triggers this lint:
49
50 ```rust
51 #[deprecated]
52 fn foo() {}
53
54 fn bar() {
55     foo();
56 }
57 ```
58
59 This will produce:
60
61 ```text
62 warning: use of deprecated item 'foo'
63  --> src/lib.rs:7:5
64   |
65 7 |     foo();
66   |     ^^^
67   |
68 ```
69
70 ## illegal-floating-point-literal-pattern
71
72 This lint detects floating-point literals used in patterns. Some example code
73 that triggers this lint:
74
75 ```rust
76 let x = 42.0;
77
78 match x {
79     5.0 => {},
80     _ => {},
81 }
82 ```
83
84 This will produce:
85
86 ```text
87 warning: floating-point literals cannot be used in patterns
88  --> src/main.rs:4:9
89   |
90 4 |         5.0 => {},
91   |         ^^^
92   |
93   = note: #[warn(illegal_floating_point_literal_pattern)] on by default
94   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
95   = note: for more information, see issue #41620 <https://github.com/rust-lang/rust/issues/41620>
96 ```
97
98 ## improper-ctypes
99
100 This lint detects proper use of libc types in foreign modules. Some
101 example code that triggers this lint:
102
103 ```rust
104 extern "C" {
105     static STATIC: String;
106 }
107 ```
108
109 This will produce:
110
111 ```text
112 warning: found struct without foreign-function-safe representation annotation in foreign module, consider adding a #[repr(C)] attribute to the type
113  --> src/main.rs:2:20
114   |
115 2 |     static STATIC: String;
116   |                    ^^^^^^
117   |
118 ```
119
120 ## late-bound-lifetime-arguments
121
122 This lint detects generic lifetime arguments in path segments with
123 late bound lifetime parameters. Some example code that triggers this lint:
124
125 ```rust
126 struct S;
127
128 impl S {
129     fn late<'a, 'b>(self, _: &'a u8, _: &'b u8) {}
130 }
131
132 fn main() {
133     S.late::<'static>(&0, &0);
134 }
135 ```
136
137 This will produce:
138
139 ```text
140 warning: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
141  --> src/main.rs:8:14
142   |
143 4 |     fn late<'a, 'b>(self, _: &'a u8, _: &'b u8) {}
144   |             -- the late bound lifetime parameter is introduced here
145 ...
146 8 |     S.late::<'static>(&0, &0);
147   |              ^^^^^^^
148   |
149   = note: #[warn(late_bound_lifetime_arguments)] on by default
150   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
151   = note: for more information, see issue #42868 <https://github.com/rust-lang/rust/issues/42868>
152 ```
153
154 ## non-camel-case-types
155
156 This lint detects types, variants, traits and type parameters that don't have
157 camel case names. Some example code that triggers this lint:
158
159 ```rust
160 struct s;
161 ```
162
163 This will produce:
164
165 ```text
166 warning: type `s` should have a camel case name such as `S`
167  --> src/main.rs:1:1
168   |
169 1 | struct s;
170   | ^^^^^^^^^
171   |
172 ```
173
174 ## non-shorthand-field-patterns
175
176 This lint detects using `Struct { x: x }` instead of `Struct { x }` in a pattern. Some
177 example code that triggers this lint:
178
179 ```rust
180 struct Point {
181     x: i32,
182     y: i32,
183 }
184
185
186 fn main() {
187     let p = Point {
188         x: 5,
189         y: 5,
190     };
191
192     match p {
193         Point { x: x, y: y } => (),
194     }
195 }
196 ```
197
198 This will produce:
199
200 ```text
201 warning: the `x:` in this pattern is redundant
202   --> src/main.rs:14:17
203    |
204 14 |         Point { x: x, y: y } => (),
205    |                 --^^
206    |                 |
207    |                 help: remove this
208    |
209
210 warning: the `y:` in this pattern is redundant
211   --> src/main.rs:14:23
212    |
213 14 |         Point { x: x, y: y } => (),
214    |                       --^^
215    |                       |
216    |                       help: remove this
217
218 ```
219
220 ## non-snake-case
221
222 This lint detects variables, methods, functions, lifetime parameters and
223 modules that don't have snake case names. Some example code that triggers
224 this lint:
225
226 ```rust
227 let X = 5;
228 ```
229
230 This will produce:
231
232 ```text
233 warning: variable `X` should have a snake case name such as `x`
234  --> src/main.rs:2:9
235   |
236 2 |     let X = 5;
237   |         ^
238   |
239 ```
240
241 ## non-upper-case-globals
242
243 This lint detects static constants that don't have uppercase identifiers.
244 Some example code that triggers this lint:
245
246 ```rust
247 static x: i32 = 5;
248 ```
249
250 This will produce:
251
252 ```text
253 warning: static variable `x` should have an upper case name such as `X`
254  --> src/main.rs:1:1
255   |
256 1 | static x: i32 = 5;
257   | ^^^^^^^^^^^^^^^^^^
258   |
259 ```
260
261 ## no-mangle-generic-items
262
263 This lint detects generic items must be mangled. Some
264 example code that triggers this lint:
265
266 ```rust
267 #[no_mangle]
268 fn foo<T>(t: T) {
269
270 }
271 ```
272
273 This will produce:
274
275 ```text
276 warning: functions generic over types must be mangled
277  --> src/main.rs:2:1
278   |
279 1 |   #[no_mangle]
280   |   ------------ help: remove this attribute
281 2 | / fn foo<T>(t: T) {
282 3 | |
283 4 | | }
284   | |_^
285   |
286 ```
287
288 ## path-statements
289
290 This lint detects path statements with no effect. Some example code that
291 triggers this lint:
292
293 ```rust
294 let x = 42;
295
296 x;
297 ```
298
299 This will produce:
300
301 ```text
302 warning: path statement with no effect
303  --> src/main.rs:3:5
304   |
305 3 |     x;
306   |     ^^
307   |
308 ```
309
310 ## patterns-in-fns-without-body
311
312 This lint detects patterns in functions without body were that were
313 previously erroneously allowed. Some example code that triggers this lint:
314
315 ```rust
316 trait Trait {
317     fn foo(mut arg: u8);
318 }
319 ```
320
321 This will produce:
322
323 ```text
324 warning: patterns aren't allowed in methods without bodies
325  --> src/main.rs:2:12
326   |
327 2 |     fn foo(mut arg: u8);
328   |            ^^^^^^^
329   |
330   = note: #[warn(patterns_in_fns_without_body)] on by default
331   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
332   = note: for more information, see issue #35203 <https://github.com/rust-lang/rust/issues/35203>
333 ```
334
335 To fix this, remove the pattern; it can be used in the implementation without
336 being used in the definition. That is:
337
338 ```rust
339 trait Trait {
340     fn foo(arg: u8);
341 }
342
343 impl Trait for i32 {
344     fn foo(mut arg: u8) {
345
346     }
347 }
348 ```
349
350 ## plugin-as-library
351
352 This lint detects when compiler plugins are used as ordinary library in
353 non-plugin crate. Some example code that triggers this lint:
354
355 ```rust,ignore
356 #![feature(plugin)]
357 #![plugin(macro_crate_test)]
358
359 extern crate macro_crate_test;
360 ```
361
362 ## private-in-public
363
364 This lint detects private items in public interfaces not caught by the old implementation. Some
365 example code that triggers this lint:
366
367 ```rust,ignore
368 pub trait Trait {
369     type A;
370 }
371
372 pub struct S;
373
374 mod foo {
375     struct Z;
376
377     impl ::Trait for ::S {
378         type A = Z;
379     }
380 }
381 # fn main() {}
382 ```
383
384 This will produce:
385
386 ```text
387 error[E0446]: private type `foo::Z` in public interface
388   --> src/main.rs:11:9
389    |
390 11 |         type A = Z;
391    |         ^^^^^^^^^^^ can't leak private type
392 ```
393
394 ## private-no-mangle-fns
395
396 This lint detects functions marked `#[no_mangle]` that are also private.
397 Given that private functions aren't exposed publicly, and `#[no_mangle]`
398 controls the public symbol, this combination is erroneous. Some example code
399 that triggers this lint:
400
401 ```rust
402 #[no_mangle]
403 fn foo() {}
404 ```
405
406 This will produce:
407
408 ```text
409 warning: function is marked #[no_mangle], but not exported
410  --> src/main.rs:2:1
411   |
412 2 | fn foo() {}
413   | -^^^^^^^^^^
414   | |
415   | help: try making it public: `pub`
416   |
417 ```
418
419 To fix this, either make it public or remove the `#[no_mangle]`.
420
421 ## private-no-mangle-statics
422
423 This lint detects any statics marked `#[no_mangle]` that are private.
424 Given that private statics aren't exposed publicly, and `#[no_mangle]`
425 controls the public symbol, this combination is erroneous. Some example code
426 that triggers this lint:
427
428 ```rust
429 #[no_mangle]
430 static X: i32 = 4;
431 ```
432
433 This will produce:
434
435 ```text
436 warning: static is marked #[no_mangle], but not exported
437  --> src/main.rs:2:1
438   |
439 2 | static X: i32 = 4;
440   | -^^^^^^^^^^^^^^^^^
441   | |
442   | help: try making it public: `pub`
443   |
444 ```
445
446 To fix this, either make it public or remove the `#[no_mangle]`.
447
448 ## renamed-and-removed-lints
449
450 This lint detects lints that have been renamed or removed. Some
451 example code that triggers this lint:
452
453 ```rust
454 #![deny(raw_pointer_derive)]
455 ```
456
457 This will produce:
458
459 ```text
460 warning: lint raw_pointer_derive has been removed: using derive with raw pointers is ok
461  --> src/main.rs:1:9
462   |
463 1 | #![deny(raw_pointer_derive)]
464   |         ^^^^^^^^^^^^^^^^^^
465   |
466 ```
467
468 To fix this, either remove the lint or use the new name.
469
470 ## safe-packed-borrows
471
472 This lint detects borrowing a field in the interior of a packed structure
473 with alignment other than 1. Some example code that triggers this lint:
474
475 ```rust
476 #[repr(packed)]
477 pub struct Unaligned<T>(pub T);
478
479 pub struct Foo {
480     start: u8,
481     data: Unaligned<u32>,
482 }
483
484 fn main() {
485     let x = Foo { start: 0, data: Unaligned(1) };
486     let y = &x.data.0;
487 }
488 ```
489
490 This will produce:
491
492 ```text
493 warning: borrow of packed field requires unsafe function or block (error E0133)
494   --> src/main.rs:11:13
495    |
496 11 |     let y = &x.data.0;
497    |             ^^^^^^^^^
498    |
499    = note: #[warn(safe_packed_borrows)] on by default
500    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
501    = note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
502 ```
503
504 ## stable-features
505
506 This lint detects a `#[feature]` attribute that's since been made stable. Some
507 example code that triggers this lint:
508
509 ```rust
510 #![feature(test_accepted_feature)]
511 ```
512
513 This will produce:
514
515 ```text
516 warning: this feature has been stable since 1.0.0. Attribute no longer needed
517  --> src/main.rs:1:12
518   |
519 1 | #![feature(test_accepted_feature)]
520   |            ^^^^^^^^^^^^^^^^^^^^^
521   |
522 ```
523
524 To fix, simply remove the `#![feature]` attribute, as it's no longer needed.
525
526 ## type-alias-bounds
527
528 This lint detects bounds in type aliases. These are not currently enforced.
529 Some example code that triggers this lint:
530
531 ```rust
532 #[allow(dead_code)]
533 type SendVec<T: Send> = Vec<T>;
534 ```
535
536 This will produce:
537
538 ```text
539 warning: bounds on generic parameters are not enforced in type aliases
540  --> src/lib.rs:2:17
541   |
542 2 | type SendVec<T: Send> = Vec<T>;
543   |                 ^^^^
544   |
545   = note: #[warn(type_alias_bounds)] on by default
546   = help: the bound will not be checked when the type alias is used, and should be removed
547 ```
548
549 ## tyvar-behind-raw-pointer
550
551 This lint detects raw pointer to an inference variable. Some
552 example code that triggers this lint:
553
554 ```rust
555 let data = std::ptr::null();
556 let _ = &data as *const *const ();
557
558 if data.is_null() {}
559 ```
560
561 This will produce:
562
563 ```text
564 warning: type annotations needed
565  --> src/main.rs:4:13
566   |
567 4 |     if data.is_null() {}
568   |             ^^^^^^^
569   |
570   = note: #[warn(tyvar_behind_raw_pointer)] on by default
571   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
572   = note: for more information, see issue #46906 <https://github.com/rust-lang/rust/issues/46906>
573 ```
574
575 ## unconditional-recursion
576
577 This lint detects functions that cannot return without calling themselves.
578 Some example code that triggers this lint:
579
580 ```rust
581 fn foo() {
582     foo();
583 }
584 ```
585
586 This will produce:
587
588 ```text
589 warning: function cannot return without recursing
590  --> src/main.rs:1:1
591   |
592 1 | fn foo() {
593   | ^^^^^^^^ cannot return without recursing
594 2 |     foo();
595   |     ----- recursive call site
596   |
597 ```
598
599 ## unions-with-drop-fields
600
601 This lint detects use of unions that contain fields with possibly non-trivial drop code. Some
602 example code that triggers this lint:
603
604 ```rust
605 #![feature(untagged_unions)]
606
607 union U {
608     s: String,
609 }
610 ```
611
612 This will produce:
613
614 ```text
615 warning: union contains a field with possibly non-trivial drop code, drop code of union fields is ignored when dropping the union
616  --> src/main.rs:4:5
617   |
618 4 |     s: String,
619   |     ^^^^^^^^^
620   |
621 ```
622
623 ## unknown-lints
624
625 This lint detects unrecognized lint attribute. Some
626 example code that triggers this lint:
627
628 ```rust,ignore
629 #[allow(not_a_real_lint)]
630 ```
631
632 This will produce:
633
634 ```text
635 warning: unknown lint: `not_a_real_lint`
636  --> src/main.rs:1:10
637   |
638 1 | #![allow(not_a_real_lint)]
639   |          ^^^^^^^^^^^^^^^
640   |
641 ```
642
643 ## unreachable-code
644
645 This lint detects unreachable code paths. Some example code that
646 triggers this lint:
647
648 ```rust,no_run
649 panic!("we never go past here!");
650
651 let x = 5;
652 ```
653
654 This will produce:
655
656 ```text
657 warning: unreachable statement
658  --> src/main.rs:4:5
659   |
660 4 |     let x = 5;
661   |     ^^^^^^^^^^
662   |
663 ```
664
665 ## unreachable-patterns
666
667 This lint detects unreachable patterns. Some
668 example code that triggers this lint:
669
670 ```rust
671 let x = 5;
672 match x {
673     y => (),
674     5 => (),
675 }
676 ```
677
678 This will produce:
679
680 ```text
681 warning: unreachable pattern
682  --> src/main.rs:5:5
683   |
684 5 |     5 => (),
685   |     ^
686   |
687 ```
688
689 The `y` pattern will always match, so the five is impossible to reach.
690 Remember, match arms match in order, you probably wanted to put the `5` case
691 above the `y` case.
692
693 ## unstable-name-collision
694
695 This lint detects that you've used a name that the standard library plans to
696 add in the future, which means that your code may fail to compile without
697 additional type annotations in the future. Either rename, or add those
698 annotations now.
699
700 ## unused-allocation
701
702 This lint detects unnecessary allocations that can be eliminated.
703
704 ## unused-assignments
705
706 This lint detects assignments that will never be read. Some
707 example code that triggers this lint:
708
709 ```rust
710 let mut x = 5;
711 x = 6;
712 ```
713
714 This will produce:
715
716 ```text
717 warning: value assigned to `x` is never read
718  --> src/main.rs:4:5
719   |
720 4 |     x = 6;
721   |     ^
722   |
723 ```
724
725 ## unused-attributes
726
727 This lint detects attributes that were not used by the compiler. Some
728 example code that triggers this lint:
729
730 ```rust
731 #![feature(custom_attribute)]
732
733 #![mutable_doc]
734 ```
735
736 This will produce:
737
738 ```text
739 warning: unused attribute
740  --> src/main.rs:4:1
741   |
742 4 | #![mutable_doc]
743   | ^^^^^^^^^^^^^^^
744   |
745 ```
746
747 ## unused-comparisons
748
749 This lint detects comparisons made useless by limits of the types involved. Some
750 example code that triggers this lint:
751
752 ```rust
753 fn foo(x: u8) {
754     x >= 0;
755 }
756 ```
757
758 This will produce:
759
760 ```text
761 warning: comparison is useless due to type limits
762  --> src/main.rs:6:5
763   |
764 6 |     x >= 0;
765   |     ^^^^^^
766   |
767 ```
768
769 ## unused-doc-comment
770
771 This lint detects doc comments that aren't used by rustdoc. Some
772 example code that triggers this lint:
773
774 ```rust
775 /// docs for x
776 let x = 12;
777 ```
778
779 This will produce:
780
781 ```text
782 warning: doc comment not used by rustdoc
783  --> src/main.rs:2:5
784   |
785 2 |     /// docs for x
786   |     ^^^^^^^^^^^^^^
787   |
788 ```
789
790 ## unused-features
791
792 This lint detects unused or unknown features found in crate-level #[feature] directives.
793 To fix this, simply remove the feature flag.
794
795 ## unused-imports
796
797 This lint detects imports that are never used. Some
798 example code that triggers this lint:
799
800 ```rust
801 use std::collections::HashMap;
802 ```
803
804 This will produce:
805
806 ```text
807 warning: unused import: `std::collections::HashMap`
808  --> src/main.rs:1:5
809   |
810 1 | use std::collections::HashMap;
811   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
812   |
813 ```
814
815 ## unused-macros
816
817 This lint detects macros that were not used. Some example code that
818 triggers this lint:
819
820 ```rust
821 macro_rules! unused {
822     () => {};
823 }
824
825 fn main() {
826 }
827 ```
828
829 This will produce:
830
831 ```text
832 warning: unused macro definition
833  --> src/main.rs:1:1
834   |
835 1 | / macro_rules! unused {
836 2 | |     () => {};
837 3 | | }
838   | |_^
839   |
840 ```
841
842 ## unused-must-use
843
844 This lint detects unused result of a type flagged as #[must_use]. Some
845 example code that triggers this lint:
846
847 ```rust
848 fn returns_result() -> Result<(), ()> {
849     Ok(())
850 }
851
852 fn main() {
853     returns_result();
854 }
855 ```
856
857 This will produce:
858
859 ```text
860 warning: unused `std::result::Result` that must be used
861  --> src/main.rs:6:5
862   |
863 6 |     returns_result();
864   |     ^^^^^^^^^^^^^^^^^
865   |
866 ```
867
868 ## unused-mut
869
870 This lint detects mut variables which don't need to be mutable. Some
871 example code that triggers this lint:
872
873 ```rust
874 let mut x = 5;
875 ```
876
877 This will produce:
878
879 ```text
880 warning: variable does not need to be mutable
881  --> src/main.rs:2:9
882   |
883 2 |     let mut x = 5;
884   |         ----^
885   |         |
886   |         help: remove this `mut`
887   |
888 ```
889
890 ## unused-parens
891
892 This lint detects `if`, `match`, `while` and `return` with parentheses; they
893 do not need them. Some example code that triggers this lint:
894
895 ```rust
896 if(true) {}
897 ```
898
899 This will produce:
900
901 ```text
902 warning: unnecessary parentheses around `if` condition
903  --> src/main.rs:2:7
904   |
905 2 |     if(true) {}
906   |       ^^^^^^ help: remove these parentheses
907   |
908 ```
909
910 ## unused-unsafe
911
912 This lint detects unnecessary use of an `unsafe` block. Some
913 example code that triggers this lint:
914
915 ```rust
916 unsafe {}
917 ```
918
919 This will produce:
920
921 ```text
922 warning: unnecessary `unsafe` block
923  --> src/main.rs:2:5
924   |
925 2 |     unsafe {}
926   |     ^^^^^^ unnecessary `unsafe` block
927   |
928 ```
929
930 ## unused-variables
931
932 This lint detects variables which are not used in any way. Some
933 example code that triggers this lint:
934
935 ```rust
936 let x = 5;
937 ```
938
939 This will produce:
940
941 ```text
942 warning: unused variable: `x`
943  --> src/main.rs:2:9
944   |
945 2 |     let x = 5;
946   |         ^ help: consider using `_x` instead
947   |
948 ```
949
950 ## warnings
951
952 This lint is a bit special; by changing its level, you change every other warning
953 that would produce a warning to whatever value you'd like:
954
955 ```rust
956 #![deny(warnings)]
957 ```
958
959 As such, you won't ever trigger this lint in your code directly.
960
961 ## while-true
962
963 This lint detects `while true { }`. Some example code that triggers this
964 lint:
965
966 ```rust,no_run
967 while true {
968
969 }
970 ```
971
972 This will produce:
973
974 ```text
975 warning: denote infinite loops with `loop { ... }`
976  --> src/main.rs:2:5
977   |
978 2 |     while true {
979   |     ^^^^^^^^^^ help: use `loop`
980   |
981 ```