forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlints.rs
2890 lines (2588 loc) · 76.5 KB
/
lints.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::untranslatable_diagnostic)]
use std::num::NonZero;
use crate::errors::RequestedLevel;
use crate::fluent_generated as fluent;
use rustc_errors::{
codes::*, Applicability, Diag, DiagArgValue, DiagMessage, DiagStyledString,
ElidedLifetimeInPathSubdiag, EmissionGuarantee, LintDiagnostic, MultiSpan, SubdiagMessageOp,
Subdiagnostic, SuggestionStyle,
};
use rustc_hir::{def::Namespace, def_id::DefId};
use rustc_macros::{LintDiagnostic, Subdiagnostic};
use rustc_middle::ty::{
inhabitedness::InhabitedPredicate, Clause, PolyExistentialTraitRef, Ty, TyCtxt,
};
use rustc_session::{lint::AmbiguityErrorDiag, Session};
use rustc_span::{
edition::Edition,
sym,
symbol::{Ident, MacroRulesNormalizedIdent},
Span, Symbol,
};
use crate::{
builtin::InitError, builtin::TypeAliasBounds, errors::OverruledAttributeSub, LateContext,
};
// array_into_iter.rs
#[derive(LintDiagnostic)]
#[diag(lint_shadowed_into_iter)]
pub struct ShadowedIntoIterDiag {
pub target: &'static str,
pub edition: &'static str,
#[suggestion(lint_use_iter_suggestion, code = "iter", applicability = "machine-applicable")]
pub suggestion: Span,
#[subdiagnostic]
pub sub: Option<ShadowedIntoIterDiagSub>,
}
#[derive(Subdiagnostic)]
pub enum ShadowedIntoIterDiagSub {
#[suggestion(lint_remove_into_iter_suggestion, code = "", applicability = "maybe-incorrect")]
RemoveIntoIter {
#[primary_span]
span: Span,
},
#[multipart_suggestion(
lint_use_explicit_into_iter_suggestion,
applicability = "maybe-incorrect"
)]
UseExplicitIntoIter {
#[suggestion_part(code = "IntoIterator::into_iter(")]
start_span: Span,
#[suggestion_part(code = ")")]
end_span: Span,
},
}
// builtin.rs
#[derive(LintDiagnostic)]
#[diag(lint_builtin_while_true)]
pub struct BuiltinWhileTrue {
#[suggestion(style = "short", code = "{replace}", applicability = "machine-applicable")]
pub suggestion: Span,
pub replace: String,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_box_pointers)]
pub struct BuiltinBoxPointers<'a> {
pub ty: Ty<'a>,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_non_shorthand_field_patterns)]
pub struct BuiltinNonShorthandFieldPatterns {
pub ident: Ident,
#[suggestion(code = "{prefix}{ident}", applicability = "machine-applicable")]
pub suggestion: Span,
pub prefix: &'static str,
}
#[derive(LintDiagnostic)]
pub enum BuiltinUnsafe {
#[diag(lint_builtin_allow_internal_unsafe)]
AllowInternalUnsafe,
#[diag(lint_builtin_unsafe_block)]
UnsafeBlock,
#[diag(lint_builtin_unsafe_trait)]
UnsafeTrait,
#[diag(lint_builtin_unsafe_impl)]
UnsafeImpl,
#[diag(lint_builtin_no_mangle_fn)]
#[note(lint_builtin_overridden_symbol_name)]
NoMangleFn,
#[diag(lint_builtin_export_name_fn)]
#[note(lint_builtin_overridden_symbol_name)]
ExportNameFn,
#[diag(lint_builtin_link_section_fn)]
#[note(lint_builtin_overridden_symbol_section)]
LinkSectionFn,
#[diag(lint_builtin_no_mangle_static)]
#[note(lint_builtin_overridden_symbol_name)]
NoMangleStatic,
#[diag(lint_builtin_export_name_static)]
#[note(lint_builtin_overridden_symbol_name)]
ExportNameStatic,
#[diag(lint_builtin_link_section_static)]
#[note(lint_builtin_overridden_symbol_section)]
LinkSectionStatic,
#[diag(lint_builtin_no_mangle_method)]
#[note(lint_builtin_overridden_symbol_name)]
NoMangleMethod,
#[diag(lint_builtin_export_name_method)]
#[note(lint_builtin_overridden_symbol_name)]
ExportNameMethod,
#[diag(lint_builtin_decl_unsafe_fn)]
DeclUnsafeFn,
#[diag(lint_builtin_decl_unsafe_method)]
DeclUnsafeMethod,
#[diag(lint_builtin_impl_unsafe_method)]
ImplUnsafeMethod,
#[diag(lint_builtin_global_asm)]
#[note(lint_builtin_global_macro_unsafety)]
GlobalAsm,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_missing_doc)]
pub struct BuiltinMissingDoc<'a> {
pub article: &'a str,
pub desc: &'a str,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_missing_copy_impl)]
pub struct BuiltinMissingCopyImpl;
pub struct BuiltinMissingDebugImpl<'a> {
pub tcx: TyCtxt<'a>,
pub def_id: DefId,
}
// Needed for def_path_str
impl<'a> LintDiagnostic<'a, ()> for BuiltinMissingDebugImpl<'_> {
fn decorate_lint<'b>(self, diag: &'b mut rustc_errors::Diag<'a, ()>) {
diag.primary_message(fluent::lint_builtin_missing_debug_impl);
diag.arg("debug", self.tcx.def_path_str(self.def_id));
}
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_anonymous_params)]
pub struct BuiltinAnonymousParams<'a> {
#[suggestion(code = "_: {ty_snip}")]
pub suggestion: (Span, Applicability),
pub ty_snip: &'a str,
}
// FIXME(davidtwco) translatable deprecated attr
#[derive(LintDiagnostic)]
#[diag(lint_builtin_deprecated_attr_link)]
pub struct BuiltinDeprecatedAttrLink<'a> {
pub name: Symbol,
pub reason: &'a str,
pub link: &'a str,
#[subdiagnostic]
pub suggestion: BuiltinDeprecatedAttrLinkSuggestion<'a>,
}
#[derive(Subdiagnostic)]
pub enum BuiltinDeprecatedAttrLinkSuggestion<'a> {
#[suggestion(lint_msg_suggestion, code = "", applicability = "machine-applicable")]
Msg {
#[primary_span]
suggestion: Span,
msg: &'a str,
},
#[suggestion(lint_default_suggestion, code = "", applicability = "machine-applicable")]
Default {
#[primary_span]
suggestion: Span,
},
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_deprecated_attr_used)]
pub struct BuiltinDeprecatedAttrUsed {
pub name: String,
#[suggestion(
lint_builtin_deprecated_attr_default_suggestion,
style = "short",
code = "",
applicability = "machine-applicable"
)]
pub suggestion: Span,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_unused_doc_comment)]
pub struct BuiltinUnusedDocComment<'a> {
pub kind: &'a str,
#[label]
pub label: Span,
#[subdiagnostic]
pub sub: BuiltinUnusedDocCommentSub,
}
#[derive(Subdiagnostic)]
pub enum BuiltinUnusedDocCommentSub {
#[help(lint_plain_help)]
PlainHelp,
#[help(lint_block_help)]
BlockHelp,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_no_mangle_generic)]
pub struct BuiltinNoMangleGeneric {
// Use of `#[no_mangle]` suggests FFI intent; correct
// fix may be to monomorphize source by hand
#[suggestion(style = "short", code = "", applicability = "maybe-incorrect")]
pub suggestion: Span,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_const_no_mangle)]
pub struct BuiltinConstNoMangle {
#[suggestion(code = "pub static", applicability = "machine-applicable")]
pub suggestion: Span,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_mutable_transmutes)]
pub struct BuiltinMutablesTransmutes;
#[derive(LintDiagnostic)]
#[diag(lint_builtin_unstable_features)]
pub struct BuiltinUnstableFeatures;
// lint_ungated_async_fn_track_caller
pub struct BuiltinUngatedAsyncFnTrackCaller<'a> {
pub label: Span,
pub session: &'a Session,
}
impl<'a> LintDiagnostic<'a, ()> for BuiltinUngatedAsyncFnTrackCaller<'_> {
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
diag.primary_message(fluent::lint_ungated_async_fn_track_caller);
diag.span_label(self.label, fluent::lint_label);
rustc_session::parse::add_feature_diagnostics(
diag,
self.session,
sym::async_fn_track_caller,
);
}
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_unreachable_pub)]
pub struct BuiltinUnreachablePub<'a> {
pub what: &'a str,
#[suggestion(code = "pub(crate)")]
pub suggestion: (Span, Applicability),
#[help]
pub help: Option<()>,
}
pub struct SuggestChangingAssocTypes<'a, 'b> {
pub ty: &'a rustc_hir::Ty<'b>,
}
impl<'a, 'b> Subdiagnostic for SuggestChangingAssocTypes<'a, 'b> {
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
self,
diag: &mut Diag<'_, G>,
_f: &F,
) {
// Access to associates types should use `<T as Bound>::Assoc`, which does not need a
// bound. Let's see if this type does that.
// We use a HIR visitor to walk the type.
use rustc_hir::intravisit::{self, Visitor};
struct WalkAssocTypes<'a, 'b, G: EmissionGuarantee> {
err: &'a mut Diag<'b, G>,
}
impl<'a, 'b, G: EmissionGuarantee> Visitor<'_> for WalkAssocTypes<'a, 'b, G> {
fn visit_qpath(
&mut self,
qpath: &rustc_hir::QPath<'_>,
id: rustc_hir::HirId,
span: Span,
) {
if TypeAliasBounds::is_type_variable_assoc(qpath) {
self.err.span_help(span, fluent::lint_builtin_type_alias_bounds_help);
}
intravisit::walk_qpath(self, qpath, id)
}
}
// Let's go for a walk!
let mut visitor = WalkAssocTypes { err: diag };
visitor.visit_ty(self.ty);
}
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_type_alias_where_clause)]
pub struct BuiltinTypeAliasWhereClause<'a, 'b> {
#[suggestion(code = "", applicability = "machine-applicable")]
pub suggestion: Span,
#[subdiagnostic]
pub sub: Option<SuggestChangingAssocTypes<'a, 'b>>,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_type_alias_generic_bounds)]
pub struct BuiltinTypeAliasGenericBounds<'a, 'b> {
#[subdiagnostic]
pub suggestion: BuiltinTypeAliasGenericBoundsSuggestion,
#[subdiagnostic]
pub sub: Option<SuggestChangingAssocTypes<'a, 'b>>,
}
pub struct BuiltinTypeAliasGenericBoundsSuggestion {
pub suggestions: Vec<(Span, String)>,
}
impl Subdiagnostic for BuiltinTypeAliasGenericBoundsSuggestion {
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
self,
diag: &mut Diag<'_, G>,
_f: &F,
) {
diag.multipart_suggestion(
fluent::lint_suggestion,
self.suggestions,
Applicability::MachineApplicable,
);
}
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_trivial_bounds)]
pub struct BuiltinTrivialBounds<'a> {
pub predicate_kind_name: &'a str,
pub predicate: Clause<'a>,
}
#[derive(LintDiagnostic)]
pub enum BuiltinEllipsisInclusiveRangePatternsLint {
#[diag(lint_builtin_ellipsis_inclusive_range_patterns)]
Parenthesise {
#[suggestion(code = "{replace}", applicability = "machine-applicable")]
suggestion: Span,
replace: String,
},
#[diag(lint_builtin_ellipsis_inclusive_range_patterns)]
NonParenthesise {
#[suggestion(style = "short", code = "..=", applicability = "machine-applicable")]
suggestion: Span,
},
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_keyword_idents)]
pub struct BuiltinKeywordIdents {
pub kw: Ident,
pub next: Edition,
#[suggestion(code = "r#{kw}", applicability = "machine-applicable")]
pub suggestion: Span,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_explicit_outlives)]
pub struct BuiltinExplicitOutlives {
pub count: usize,
#[subdiagnostic]
pub suggestion: BuiltinExplicitOutlivesSuggestion,
}
#[derive(Subdiagnostic)]
#[multipart_suggestion(lint_suggestion)]
pub struct BuiltinExplicitOutlivesSuggestion {
#[suggestion_part(code = "")]
pub spans: Vec<Span>,
#[applicability]
pub applicability: Applicability,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_incomplete_features)]
pub struct BuiltinIncompleteFeatures {
pub name: Symbol,
#[subdiagnostic]
pub note: Option<BuiltinFeatureIssueNote>,
#[subdiagnostic]
pub help: Option<BuiltinIncompleteFeaturesHelp>,
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_internal_features)]
#[note]
pub struct BuiltinInternalFeatures {
pub name: Symbol,
}
#[derive(Subdiagnostic)]
#[help(lint_help)]
pub struct BuiltinIncompleteFeaturesHelp;
#[derive(Subdiagnostic)]
#[note(lint_note)]
pub struct BuiltinFeatureIssueNote {
pub n: NonZero<u32>,
}
pub struct BuiltinUnpermittedTypeInit<'a> {
pub msg: DiagMessage,
pub ty: Ty<'a>,
pub label: Span,
pub sub: BuiltinUnpermittedTypeInitSub,
pub tcx: TyCtxt<'a>,
}
impl<'a> LintDiagnostic<'a, ()> for BuiltinUnpermittedTypeInit<'_> {
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
diag.primary_message(self.msg);
diag.arg("ty", self.ty);
diag.span_label(self.label, fluent::lint_builtin_unpermitted_type_init_label);
if let InhabitedPredicate::True = self.ty.inhabited_predicate(self.tcx) {
// Only suggest late `MaybeUninit::assume_init` initialization if the type is inhabited.
diag.span_label(
self.label,
fluent::lint_builtin_unpermitted_type_init_label_suggestion,
);
}
self.sub.add_to_diag(diag);
}
}
// FIXME(davidtwco): make translatable
pub struct BuiltinUnpermittedTypeInitSub {
pub err: InitError,
}
impl Subdiagnostic for BuiltinUnpermittedTypeInitSub {
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
self,
diag: &mut Diag<'_, G>,
_f: &F,
) {
let mut err = self.err;
loop {
if let Some(span) = err.span {
diag.span_note(span, err.message);
} else {
diag.note(err.message);
}
if let Some(e) = err.nested {
err = *e;
} else {
break;
}
}
}
}
#[derive(LintDiagnostic)]
pub enum BuiltinClashingExtern<'a> {
#[diag(lint_builtin_clashing_extern_same_name)]
SameName {
this: Symbol,
orig: Symbol,
#[label(lint_previous_decl_label)]
previous_decl_label: Span,
#[label(lint_mismatch_label)]
mismatch_label: Span,
#[subdiagnostic]
sub: BuiltinClashingExternSub<'a>,
},
#[diag(lint_builtin_clashing_extern_diff_name)]
DiffName {
this: Symbol,
orig: Symbol,
#[label(lint_previous_decl_label)]
previous_decl_label: Span,
#[label(lint_mismatch_label)]
mismatch_label: Span,
#[subdiagnostic]
sub: BuiltinClashingExternSub<'a>,
},
}
// FIXME(davidtwco): translatable expected/found
pub struct BuiltinClashingExternSub<'a> {
pub tcx: TyCtxt<'a>,
pub expected: Ty<'a>,
pub found: Ty<'a>,
}
impl Subdiagnostic for BuiltinClashingExternSub<'_> {
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
self,
diag: &mut Diag<'_, G>,
_f: &F,
) {
let mut expected_str = DiagStyledString::new();
expected_str.push(self.expected.fn_sig(self.tcx).to_string(), false);
let mut found_str = DiagStyledString::new();
found_str.push(self.found.fn_sig(self.tcx).to_string(), true);
diag.note_expected_found(&"", expected_str, &"", found_str);
}
}
#[derive(LintDiagnostic)]
#[diag(lint_builtin_deref_nullptr)]
pub struct BuiltinDerefNullptr {
#[label]
pub label: Span,
}
// FIXME: migrate fluent::lint::builtin_asm_labels
#[derive(LintDiagnostic)]
pub enum BuiltinSpecialModuleNameUsed {
#[diag(lint_builtin_special_module_name_used_lib)]
#[note]
#[help]
Lib,
#[diag(lint_builtin_special_module_name_used_main)]
#[note]
Main,
}
// deref_into_dyn_supertrait.rs
#[derive(LintDiagnostic)]
#[diag(lint_supertrait_as_deref_target)]
pub struct SupertraitAsDerefTarget<'a> {
pub self_ty: Ty<'a>,
pub supertrait_principal: PolyExistentialTraitRef<'a>,
pub target_principal: PolyExistentialTraitRef<'a>,
#[label]
pub label: Span,
#[subdiagnostic]
pub label2: Option<SupertraitAsDerefTargetLabel>,
}
#[derive(Subdiagnostic)]
#[label(lint_label2)]
pub struct SupertraitAsDerefTargetLabel {
#[primary_span]
pub label: Span,
}
// enum_intrinsics_non_enums.rs
#[derive(LintDiagnostic)]
#[diag(lint_enum_intrinsics_mem_discriminant)]
pub struct EnumIntrinsicsMemDiscriminate<'a> {
pub ty_param: Ty<'a>,
#[note]
pub note: Span,
}
#[derive(LintDiagnostic)]
#[diag(lint_enum_intrinsics_mem_variant)]
#[note]
pub struct EnumIntrinsicsMemVariant<'a> {
pub ty_param: Ty<'a>,
}
// expect.rs
#[derive(LintDiagnostic)]
#[diag(lint_expectation)]
pub struct Expectation {
#[subdiagnostic]
pub rationale: Option<ExpectationNote>,
#[note]
pub note: Option<()>,
}
#[derive(Subdiagnostic)]
#[note(lint_rationale)]
pub struct ExpectationNote {
pub rationale: Symbol,
}
// ptr_nulls.rs
#[derive(LintDiagnostic)]
pub enum PtrNullChecksDiag<'a> {
#[diag(lint_ptr_null_checks_fn_ptr)]
#[help(lint_help)]
FnPtr {
orig_ty: Ty<'a>,
#[label]
label: Span,
},
#[diag(lint_ptr_null_checks_ref)]
Ref {
orig_ty: Ty<'a>,
#[label]
label: Span,
},
#[diag(lint_ptr_null_checks_fn_ret)]
FnRet { fn_name: Ident },
}
// for_loops_over_fallibles.rs
#[derive(LintDiagnostic)]
#[diag(lint_for_loops_over_fallibles)]
pub struct ForLoopsOverFalliblesDiag<'a> {
pub article: &'static str,
pub ref_prefix: &'static str,
pub ty: &'static str,
#[subdiagnostic]
pub sub: ForLoopsOverFalliblesLoopSub<'a>,
#[subdiagnostic]
pub question_mark: Option<ForLoopsOverFalliblesQuestionMark>,
#[subdiagnostic]
pub suggestion: ForLoopsOverFalliblesSuggestion<'a>,
}
#[derive(Subdiagnostic)]
pub enum ForLoopsOverFalliblesLoopSub<'a> {
#[suggestion(lint_remove_next, code = ".by_ref()", applicability = "maybe-incorrect")]
RemoveNext {
#[primary_span]
suggestion: Span,
recv_snip: String,
},
#[multipart_suggestion(lint_use_while_let, applicability = "maybe-incorrect")]
UseWhileLet {
#[suggestion_part(code = "while let {var}(")]
start_span: Span,
#[suggestion_part(code = ") = ")]
end_span: Span,
var: &'a str,
},
}
#[derive(Subdiagnostic)]
#[suggestion(lint_use_question_mark, code = "?", applicability = "maybe-incorrect")]
pub struct ForLoopsOverFalliblesQuestionMark {
#[primary_span]
pub suggestion: Span,
}
#[derive(Subdiagnostic)]
#[multipart_suggestion(lint_suggestion, applicability = "maybe-incorrect")]
pub struct ForLoopsOverFalliblesSuggestion<'a> {
pub var: &'a str,
#[suggestion_part(code = "if let {var}(")]
pub start_span: Span,
#[suggestion_part(code = ") = ")]
pub end_span: Span,
}
#[derive(Subdiagnostic)]
pub enum UseLetUnderscoreIgnoreSuggestion {
#[note(lint_use_let_underscore_ignore_suggestion)]
Note,
#[multipart_suggestion(
lint_use_let_underscore_ignore_suggestion,
style = "verbose",
applicability = "maybe-incorrect"
)]
Suggestion {
#[suggestion_part(code = "let _ = ")]
start_span: Span,
#[suggestion_part(code = "")]
end_span: Span,
},
}
// drop_forget_useless.rs
#[derive(LintDiagnostic)]
#[diag(lint_dropping_references)]
pub struct DropRefDiag<'a> {
pub arg_ty: Ty<'a>,
#[label]
pub label: Span,
#[subdiagnostic]
pub sugg: UseLetUnderscoreIgnoreSuggestion,
}
#[derive(LintDiagnostic)]
#[diag(lint_dropping_copy_types)]
pub struct DropCopyDiag<'a> {
pub arg_ty: Ty<'a>,
#[label]
pub label: Span,
#[subdiagnostic]
pub sugg: UseLetUnderscoreIgnoreSuggestion,
}
#[derive(LintDiagnostic)]
#[diag(lint_forgetting_references)]
pub struct ForgetRefDiag<'a> {
pub arg_ty: Ty<'a>,
#[label]
pub label: Span,
#[subdiagnostic]
pub sugg: UseLetUnderscoreIgnoreSuggestion,
}
#[derive(LintDiagnostic)]
#[diag(lint_forgetting_copy_types)]
pub struct ForgetCopyDiag<'a> {
pub arg_ty: Ty<'a>,
#[label]
pub label: Span,
#[subdiagnostic]
pub sugg: UseLetUnderscoreIgnoreSuggestion,
}
#[derive(LintDiagnostic)]
#[diag(lint_undropped_manually_drops)]
pub struct UndroppedManuallyDropsDiag<'a> {
pub arg_ty: Ty<'a>,
#[label]
pub label: Span,
#[subdiagnostic]
pub suggestion: UndroppedManuallyDropsSuggestion,
}
#[derive(Subdiagnostic)]
#[multipart_suggestion(lint_suggestion, applicability = "machine-applicable")]
pub struct UndroppedManuallyDropsSuggestion {
#[suggestion_part(code = "std::mem::ManuallyDrop::into_inner(")]
pub start_span: Span,
#[suggestion_part(code = ")")]
pub end_span: Span,
}
// invalid_from_utf8.rs
#[derive(LintDiagnostic)]
pub enum InvalidFromUtf8Diag {
#[diag(lint_invalid_from_utf8_unchecked)]
Unchecked {
method: String,
valid_up_to: usize,
#[label]
label: Span,
},
#[diag(lint_invalid_from_utf8_checked)]
Checked {
method: String,
valid_up_to: usize,
#[label]
label: Span,
},
}
// reference_casting.rs
#[derive(LintDiagnostic)]
pub enum InvalidReferenceCastingDiag<'tcx> {
#[diag(lint_invalid_reference_casting_borrow_as_mut)]
#[note(lint_invalid_reference_casting_note_book)]
BorrowAsMut {
#[label]
orig_cast: Option<Span>,
#[note(lint_invalid_reference_casting_note_ty_has_interior_mutability)]
ty_has_interior_mutability: Option<()>,
},
#[diag(lint_invalid_reference_casting_assign_to_ref)]
#[note(lint_invalid_reference_casting_note_book)]
AssignToRef {
#[label]
orig_cast: Option<Span>,
#[note(lint_invalid_reference_casting_note_ty_has_interior_mutability)]
ty_has_interior_mutability: Option<()>,
},
#[diag(lint_invalid_reference_casting_bigger_layout)]
#[note(lint_layout)]
BiggerLayout {
#[label]
orig_cast: Option<Span>,
#[label(lint_alloc)]
alloc: Span,
from_ty: Ty<'tcx>,
from_size: u64,
to_ty: Ty<'tcx>,
to_size: u64,
},
}
// hidden_unicode_codepoints.rs
#[derive(LintDiagnostic)]
#[diag(lint_hidden_unicode_codepoints)]
#[note]
pub struct HiddenUnicodeCodepointsDiag<'a> {
pub label: &'a str,
pub count: usize,
#[label]
pub span_label: Span,
#[subdiagnostic]
pub labels: Option<HiddenUnicodeCodepointsDiagLabels>,
#[subdiagnostic]
pub sub: HiddenUnicodeCodepointsDiagSub,
}
pub struct HiddenUnicodeCodepointsDiagLabels {
pub spans: Vec<(char, Span)>,
}
impl Subdiagnostic for HiddenUnicodeCodepointsDiagLabels {
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
self,
diag: &mut Diag<'_, G>,
_f: &F,
) {
for (c, span) in self.spans {
diag.span_label(span, format!("{c:?}"));
}
}
}
pub enum HiddenUnicodeCodepointsDiagSub {
Escape { spans: Vec<(char, Span)> },
NoEscape { spans: Vec<(char, Span)> },
}
// Used because of multiple multipart_suggestion and note
impl Subdiagnostic for HiddenUnicodeCodepointsDiagSub {
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
self,
diag: &mut Diag<'_, G>,
_f: &F,
) {
match self {
HiddenUnicodeCodepointsDiagSub::Escape { spans } => {
diag.multipart_suggestion_with_style(
fluent::lint_suggestion_remove,
spans.iter().map(|(_, span)| (*span, "".to_string())).collect(),
Applicability::MachineApplicable,
SuggestionStyle::HideCodeAlways,
);
diag.multipart_suggestion(
fluent::lint_suggestion_escape,
spans
.into_iter()
.map(|(c, span)| {
let c = format!("{c:?}");
(span, c[1..c.len() - 1].to_string())
})
.collect(),
Applicability::MachineApplicable,
);
}
HiddenUnicodeCodepointsDiagSub::NoEscape { spans } => {
// FIXME: in other suggestions we've reversed the inner spans of doc comments. We
// should do the same here to provide the same good suggestions as we do for
// literals above.
diag.arg(
"escaped",
spans
.into_iter()
.map(|(c, _)| format!("{c:?}"))
.collect::<Vec<String>>()
.join(", "),
);
diag.note(fluent::lint_suggestion_remove);
diag.note(fluent::lint_no_suggestion_note_escape);
}
}
}
}
// map_unit_fn.rs
#[derive(LintDiagnostic)]
#[diag(lint_map_unit_fn)]
#[note]
pub struct MappingToUnit {
#[label(lint_function_label)]
pub function_label: Span,
#[label(lint_argument_label)]
pub argument_label: Span,
#[label(lint_map_label)]
pub map_label: Span,
#[suggestion(style = "verbose", code = "{replace}", applicability = "maybe-incorrect")]
pub suggestion: Span,
pub replace: String,
}
// internal.rs
#[derive(LintDiagnostic)]
#[diag(lint_default_hash_types)]
#[note]
pub struct DefaultHashTypesDiag<'a> {
pub preferred: &'a str,
pub used: Symbol,
}
#[derive(LintDiagnostic)]
#[diag(lint_query_instability)]
#[note]
pub struct QueryInstability {
pub query: Symbol,
}
#[derive(LintDiagnostic)]
#[diag(lint_span_use_eq_ctxt)]
pub struct SpanUseEqCtxtDiag;
#[derive(LintDiagnostic)]
#[diag(lint_tykind_kind)]
pub struct TykindKind {
#[suggestion(code = "ty", applicability = "maybe-incorrect")]
pub suggestion: Span,
}
#[derive(LintDiagnostic)]
#[diag(lint_tykind)]
#[help]
pub struct TykindDiag;
#[derive(LintDiagnostic)]
#[diag(lint_ty_qualified)]
pub struct TyQualified {
pub ty: String,
#[suggestion(code = "{ty}", applicability = "maybe-incorrect")]
pub suggestion: Span,
}
#[derive(LintDiagnostic)]
#[diag(lint_lintpass_by_hand)]
#[help]
pub struct LintPassByHand;
#[derive(LintDiagnostic)]
#[diag(lint_non_existent_doc_keyword)]
#[help]
pub struct NonExistentDocKeyword {
pub keyword: Symbol,
}
#[derive(LintDiagnostic)]
#[diag(lint_diag_out_of_impl)]
pub struct DiagOutOfImpl;
#[derive(LintDiagnostic)]
#[diag(lint_untranslatable_diag)]
pub struct UntranslatableDiag;
#[derive(LintDiagnostic)]
#[diag(lint_bad_opt_access)]
pub struct BadOptAccessDiag<'a> {
pub msg: &'a str,
}
// let_underscore.rs
#[derive(LintDiagnostic)]
pub enum NonBindingLet {
#[diag(lint_non_binding_let_on_sync_lock)]
SyncLock {
#[subdiagnostic]
sub: NonBindingLetSub,
},
#[diag(lint_non_binding_let_on_drop_type)]
DropType {
#[subdiagnostic]
sub: NonBindingLetSub,
},
}
pub struct NonBindingLetSub {
pub suggestion: Span,
pub drop_fn_start_end: Option<(Span, Span)>,
pub is_assign_desugar: bool,
}
impl Subdiagnostic for NonBindingLetSub {
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
self,
diag: &mut Diag<'_, G>,
_f: &F,
) {
let can_suggest_binding = self.drop_fn_start_end.is_some() || !self.is_assign_desugar;
if can_suggest_binding {
let prefix = if self.is_assign_desugar { "let " } else { "" };
diag.span_suggestion_verbose(
self.suggestion,
fluent::lint_non_binding_let_suggestion,
format!("{prefix}_unused"),
Applicability::MachineApplicable,
);
} else {
diag.span_help(self.suggestion, fluent::lint_non_binding_let_suggestion);
}
if let Some(drop_fn_start_end) = self.drop_fn_start_end {
diag.multipart_suggestion(
fluent::lint_non_binding_let_multi_suggestion,
vec![
(drop_fn_start_end.0, "drop(".to_string()),
(drop_fn_start_end.1, ")".to_string()),
],
Applicability::MachineApplicable,
);