]> git.lizzy.rs Git - rust.git/blob - src/doc/rustc/src/lints/listing/warn-by-default.md
Deny the `overflowing_literals` lint for all editions
[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 type SendVec<T: Send> = Vec<T>;
533 ```
534
535 This will produce:
536
537 ```text
538 warning: type alias is never used: `SendVec`
539  --> src/main.rs:1:1
540   |
541 1 | type SendVec<T: Send> = Vec<T>;
542   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
543   |
544 ```
545
546 ## tyvar-behind-raw-pointer
547
548 This lint detects raw pointer to an inference variable. Some
549 example code that triggers this lint:
550
551 ```rust
552 let data = std::ptr::null();
553 let _ = &data as *const *const ();
554
555 if data.is_null() {}
556 ```
557
558 This will produce:
559
560 ```text
561 warning: type annotations needed
562  --> src/main.rs:4:13
563   |
564 4 |     if data.is_null() {}
565   |             ^^^^^^^
566   |
567   = note: #[warn(tyvar_behind_raw_pointer)] on by default
568   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in the 2018 edition!
569   = note: for more information, see issue #46906 <https://github.com/rust-lang/rust/issues/46906>
570 ```
571
572 ## unconditional-recursion
573
574 This lint detects functions that cannot return without calling themselves.
575 Some example code that triggers this lint:
576
577 ```rust
578 fn foo() {
579     foo();
580 }
581 ```
582
583 This will produce:
584
585 ```text
586 warning: function cannot return without recursing
587  --> src/main.rs:1:1
588   |
589 1 | fn foo() {
590   | ^^^^^^^^ cannot return without recursing
591 2 |     foo();
592   |     ----- recursive call site
593   |
594 ```
595
596 ## unions-with-drop-fields
597
598 This lint detects use of unions that contain fields with possibly non-trivial drop code. Some
599 example code that triggers this lint:
600
601 ```rust
602 #![feature(untagged_unions)]
603
604 union U {
605     s: String,
606 }
607 ```
608
609 This will produce:
610
611 ```text
612 warning: union contains a field with possibly non-trivial drop code, drop code of union fields is ignored when dropping the union
613  --> src/main.rs:4:5
614   |
615 4 |     s: String,
616   |     ^^^^^^^^^
617   |
618 ```
619
620 ## unknown-lints
621
622 This lint detects unrecognized lint attribute. Some
623 example code that triggers this lint:
624
625 ```rust,ignore
626 #[allow(not_a_real_lint)]
627 ```
628
629 This will produce:
630
631 ```text
632 warning: unknown lint: `not_a_real_lint`
633  --> src/main.rs:1:10
634   |
635 1 | #![allow(not_a_real_lint)]
636   |          ^^^^^^^^^^^^^^^
637   |
638 ```
639
640 ## unreachable-code
641
642 This lint detects unreachable code paths. Some example code that
643 triggers this lint:
644
645 ```rust,no_run
646 panic!("we never go past here!");
647
648 let x = 5;
649 ```
650
651 This will produce:
652
653 ```text
654 warning: unreachable statement
655  --> src/main.rs:4:5
656   |
657 4 |     let x = 5;
658   |     ^^^^^^^^^^
659   |
660 ```
661
662 ## unreachable-patterns
663
664 This lint detects unreachable patterns. Some
665 example code that triggers this lint:
666
667 ```rust
668 let x = 5;
669 match x {
670     y => (),
671     5 => (),
672 }
673 ```
674
675 This will produce:
676
677 ```text
678 warning: unreachable pattern
679  --> src/main.rs:5:5
680   |
681 5 |     5 => (),
682   |     ^
683   |
684 ```
685
686 The `y` pattern will always match, so the five is impossible to reach.
687 Remember, match arms match in order, you probably wanted to put the `5` case
688 above the `y` case.
689
690 ## unstable-name-collision
691
692 This lint detects that you've used a name that the standard library plans to
693 add in the future, which means that your code may fail to compile without
694 additional type annotations in the future. Either rename, or add those
695 annotations now.
696
697 ## unused-allocation
698
699 This lint detects unnecessary allocations that can be eliminated.
700
701 ## unused-assignments
702
703 This lint detects assignments that will never be read. Some
704 example code that triggers this lint:
705
706 ```rust
707 let mut x = 5;
708 x = 6;
709 ```
710
711 This will produce:
712
713 ```text
714 warning: value assigned to `x` is never read
715  --> src/main.rs:4:5
716   |
717 4 |     x = 6;
718   |     ^
719   |
720 ```
721
722 ## unused-attributes
723
724 This lint detects attributes that were not used by the compiler. Some
725 example code that triggers this lint:
726
727 ```rust
728 #![feature(custom_attribute)]
729
730 #![mutable_doc]
731 ```
732
733 This will produce:
734
735 ```text
736 warning: unused attribute
737  --> src/main.rs:4:1
738   |
739 4 | #![mutable_doc]
740   | ^^^^^^^^^^^^^^^
741   |
742 ```
743
744 ## unused-comparisons
745
746 This lint detects comparisons made useless by limits of the types involved. Some
747 example code that triggers this lint:
748
749 ```rust
750 fn foo(x: u8) {
751     x >= 0;
752 }
753 ```
754
755 This will produce:
756
757 ```text
758 warning: comparison is useless due to type limits
759  --> src/main.rs:6:5
760   |
761 6 |     x >= 0;
762   |     ^^^^^^
763   |
764 ```
765
766 ## unused-doc-comment
767
768 This lint detects doc comments that aren't used by rustdoc. Some
769 example code that triggers this lint:
770
771 ```rust
772 /// docs for x
773 let x = 12;
774 ```
775
776 This will produce:
777
778 ```text
779 warning: doc comment not used by rustdoc
780  --> src/main.rs:2:5
781   |
782 2 |     /// docs for x
783   |     ^^^^^^^^^^^^^^
784   |
785 ```
786
787 ## unused-features
788
789 This lint detects unused or unknown features found in crate-level #[feature] directives.
790 To fix this, simply remove the feature flag.
791
792 ## unused-imports
793
794 This lint detects imports that are never used. Some
795 example code that triggers this lint:
796
797 ```rust
798 use std::collections::HashMap;
799 ```
800
801 This will produce:
802
803 ```text
804 warning: unused import: `std::collections::HashMap`
805  --> src/main.rs:1:5
806   |
807 1 | use std::collections::HashMap;
808   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
809   |
810 ```
811
812 ## unused-macros
813
814 This lint detects macros that were not used. Some example code that
815 triggers this lint:
816
817 ```rust
818 macro_rules! unused {
819     () => {};
820 }
821
822 fn main() {
823 }
824 ```
825
826 This will produce:
827
828 ```text
829 warning: unused macro definition
830  --> src/main.rs:1:1
831   |
832 1 | / macro_rules! unused {
833 2 | |     () => {};
834 3 | | }
835   | |_^
836   |
837 ```
838
839 ## unused-must-use
840
841 This lint detects unused result of a type flagged as #[must_use]. Some
842 example code that triggers this lint:
843
844 ```rust
845 fn returns_result() -> Result<(), ()> {
846     Ok(())
847 }
848
849 fn main() {
850     returns_result();
851 }
852 ```
853
854 This will produce:
855
856 ```text
857 warning: unused `std::result::Result` that must be used
858  --> src/main.rs:6:5
859   |
860 6 |     returns_result();
861   |     ^^^^^^^^^^^^^^^^^
862   |
863 ```
864
865 ## unused-mut
866
867 This lint detects mut variables which don't need to be mutable. Some
868 example code that triggers this lint:
869
870 ```rust
871 let mut x = 5;
872 ```
873
874 This will produce:
875
876 ```text
877 warning: variable does not need to be mutable
878  --> src/main.rs:2:9
879   |
880 2 |     let mut x = 5;
881   |         ----^
882   |         |
883   |         help: remove this `mut`
884   |
885 ```
886
887 ## unused-parens
888
889 This lint detects `if`, `match`, `while` and `return` with parentheses; they
890 do not need them. Some example code that triggers this lint:
891
892 ```rust
893 if(true) {}
894 ```
895
896 This will produce:
897
898 ```text
899 warning: unnecessary parentheses around `if` condition
900  --> src/main.rs:2:7
901   |
902 2 |     if(true) {}
903   |       ^^^^^^ help: remove these parentheses
904   |
905 ```
906
907 ## unused-unsafe
908
909 This lint detects unnecessary use of an `unsafe` block. Some
910 example code that triggers this lint:
911
912 ```rust
913 unsafe {}
914 ```
915
916 This will produce:
917
918 ```text
919 warning: unnecessary `unsafe` block
920  --> src/main.rs:2:5
921   |
922 2 |     unsafe {}
923   |     ^^^^^^ unnecessary `unsafe` block
924   |
925 ```
926
927 ## unused-variables
928
929 This lint detects variables which are not used in any way. Some
930 example code that triggers this lint:
931
932 ```rust
933 let x = 5;
934 ```
935
936 This will produce:
937
938 ```text
939 warning: unused variable: `x`
940  --> src/main.rs:2:9
941   |
942 2 |     let x = 5;
943   |         ^ help: consider using `_x` instead
944   |
945 ```
946
947 ## warnings
948
949 This lint is a bit special; by changing its level, you change every other warning
950 that would produce a warning to whatever value you'd like:
951
952 ```rust
953 #![deny(warnings)]
954 ```
955
956 As such, you won't ever trigger this lint in your code directly.
957
958 ## while-true
959
960 This lint detects `while true { }`. Some example code that triggers this
961 lint:
962
963 ```rust,no_run
964 while true {
965
966 }
967 ```
968
969 This will produce:
970
971 ```text
972 warning: denote infinite loops with `loop { ... }`
973  --> src/main.rs:2:5
974   |
975 2 |     while true {
976   |     ^^^^^^^^^^ help: use `loop`
977   |
978 ```