/
RsErrorAnnotatorTest.kt
3946 lines (3298 loc) · 131 KB
/
RsErrorAnnotatorTest.kt
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
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.ide.annotator
import org.rust.*
import org.rust.cargo.project.workspace.CargoWorkspace
class RsErrorAnnotatorTest : RsAnnotatorTestBase(RsErrorAnnotator::class) {
@MockRustcVersion("1.29.0")
fun `test invalid module declarations`() = checkByFileTree("""
//- main.rs
mod module;
//- module.rs
/*caret*/
<error descr="mod statements in non-mod.rs files is experimental [E0658]">mod foo;</error>
#[path="helper.rs"]
mod foobar;
#[path=""] mod <error descr="File not found for module `nonono` [E0583]">nonono</error>;
mod inner {
mod <error descr="File not found for module `bar` [E0583]">bar</error>;
}
fn foo() {
mod foo {
<error descr="Cannot declare a non-inline module inside a block unless it has a path attribute">mod bar;</error>
}
}
//- helper.rs
""")
fun `test create file quick fix 1`() = checkFixByFileTree("Create module file `foo.rs`", """
//- main.rs
mod bar;
//- bar/mod.rs
mod <error descr="File not found for module `foo` [E0583]">/*caret*/foo</error>;
fn main() {
println!("Hello, World!");
}
""", """
//- main.rs
mod bar;
//- bar/mod.rs
mod foo;
fn main() {
println!("Hello, World!");
}
//- bar/foo.rs
""")
@MockRustcVersion("1.30.0")
fun `test create file quick fix 2`() = checkFixByFileTree("Create module file `foo.rs`", """
//- main.rs
mod bar;
//- bar.rs
mod <error descr="File not found for module `foo` [E0583]">/*caret*/foo</error>;
fn main() {
println!("Hello, World!");
}
""", """
//- main.rs
mod bar;
//- bar.rs
mod foo;
fn main() {
println!("Hello, World!");
}
//- bar/foo.rs
""")
fun `test create file in subdirectory quick fix 1`() = checkFixByFileTree("Create module file `foo/mod.rs`", """
//- main.rs
mod bar;
//- bar/mod.rs
mod <error descr="File not found for module `foo` [E0583]">/*caret*/foo</error>;
fn main() {
println!("Hello, World!");
}
""", """
//- main.rs
mod bar;
//- bar/mod.rs
mod foo;
fn main() {
println!("Hello, World!");
}
//- bar/foo/mod.rs
""")
@MockRustcVersion("1.30.0")
fun `test create file in subdirectory quick fix 2`() = checkFixByFileTree("Create module file `foo/mod.rs`", """
//- main.rs
mod bar;
//- bar.rs
mod <error descr="File not found for module `foo` [E0583]">/*caret*/foo</error>;
fn main() {
println!("Hello, World!");
}
""", """
//- main.rs
mod bar;
//- bar.rs
mod foo;
fn main() {
println!("Hello, World!");
}
//- bar/foo/mod.rs
""")
fun `test create file in existing subdirectory quick fix`() = checkFixByFileTree("Create module file `bar/mod.rs`", """
//- main.rs
mod <error descr="File not found for module `bar` [E0583]">/*caret*/bar</error>;
//- bar/some_random_file_to_create_a_directory.txt
""", """
//- main.rs
mod bar;
//- bar/mod.rs
//- bar/some_random_file_to_create_a_directory.txt
""")
fun `test no E0583 if no semicolon after module declaration`() = checkByText("""
mod foo<error descr="';' or '{' expected"> </error>
// often happens during typing `mod foo {}`
""")
@MockRustcVersion("1.29.0")
fun `test create file and expand module quick fix`() = checkFixByFileTree("Create module file `bar.rs`", """
//- main.rs
mod foo;
//- foo.rs
<error descr="mod statements in non-mod.rs files is experimental [E0658]">mod bar/*caret*/;</error>
""", """
//- main.rs
mod foo;
//- foo/mod.rs
mod bar;
//- foo/bar.rs
""")
@MockRustcVersion("1.29.0")
fun `test create file in subdirectory and expand module quick fix`() = checkFixByFileTree("Create module file `bar/mod.rs`", """
//- main.rs
mod foo;
//- foo.rs
<error descr="mod statements in non-mod.rs files is experimental [E0658]">mod bar/*caret*/;</error>
""", """
//- main.rs
mod foo;
//- foo/mod.rs
mod bar;
//- foo/bar/mod.rs
""")
fun `test paths`() = checkErrors("""
fn main() {
let ok = self::super::super::foo;
let ok = super::foo::bar;
let _ = ::<error descr="Invalid path: self and super are allowed only at the beginning">self</error>::foo;
let _ = ::<error>super</error>::foo;
let _ = self::<error>self</error>;
let _ = super::<error>self</error>;
let _ = foo::<error>self</error>::bar;
let _ = self::foo::<error>super</error>::bar;
}
""")
fun `test invalid chain comparison`() = checkErrors("""
fn foo(x: i32) {
<error descr="Chained comparison operator require parentheses">1 < x < 3</error>;
<error descr="Chained comparison operator require parentheses">1 > x < 3</error>;
<error descr="Chained comparison operator require parentheses">1 > x > 3</error>;
<error descr="Chained comparison operator require parentheses">1 < x > 3</error>;
<error descr="Chained comparison operator require parentheses">1 <= x < 3</error>;
<error descr="Chained comparison operator require parentheses">1 < x <= 3</error>;
<error descr="Chained comparison operator require parentheses">1 == x < 3</error>;
<error descr="Chained comparison operator require parentheses">1 < x == 3</error>;
}
""")
fun `test valid chain comparison`() = checkErrors("""
fn foo(x: i32, y: bool) {
let _ = 1 < x && x < 10;
let _ = 1 < x || x < 10;
let _ = (1 == x) == y;
let _ = y == (1 == x);
}
""")
fun `test not applied E0046`() = checkErrors("""
trait T {
fn foo() {}
fn bar();
}
impl T for() {
fn bar() {}
}
""")
fun `test ignore macros E0046`() = checkErrors("""
trait T { fn foo(&self); }
macro_rules! impl_foo {
() => { fn foo(&self) {} };
}
struct S;
impl T for S { impl_foo!(); }
""")
fun `test invalid parameters number in closures E0057`() = checkErrors("""
fn main() {
let closure_0 = || ();
let closure_1 = |x| x;
let closure_2 = |x, y| (x, y);
closure_0();
closure_0(<error descr="This function takes 0 parameters but 1 parameter was supplied [E0057]">42</error>);
closure_1(<error descr="This function takes 1 parameter but 0 parameters were supplied [E0057]">)</error>;
closure_1(42);
closure_2(<error descr="This function takes 2 parameters but 0 parameters were supplied [E0057]">)</error>;
closure_2(42<error descr="This function takes 2 parameters but 1 parameter was supplied [E0057]">)</error>;
closure_2(42, 43);
}
""")
fun `test invalid parameters number in variadic functions E0060`() = checkErrors("""
extern {
fn variadic_1(p1: u32, ...);
fn variadic_2(p1: u32, p2: u32, ...);
}
unsafe fn test() {
variadic_1(<error descr="This function takes at least 1 parameter but 0 parameters were supplied [E0060]">)</error>;
variadic_1(42);
variadic_1(42, 43);
variadic_2(<error descr="This function takes at least 2 parameters but 0 parameters were supplied [E0060]">)</error>;
variadic_2(42<error descr="This function takes at least 2 parameters but 1 parameter was supplied [E0060]">)</error>;
variadic_2(42, 43);
}
""")
@MockAdditionalCfgOptions("intellij_rust")
fun `test invalid parameters number in free functions E0061`() = checkErrors("""
fn par_0() {}
fn par_1(p: bool) {}
fn par_3(p1: u32, p2: f64, p3: &'static str) {}
fn par_0_cfg(#[cfg(not(intellij_rust))] p1: u32) {}
fn par_1_cfg(#[cfg(intellij_rust)] p1: u32, #[cfg(not(intellij_rust))] p1: i32) {}
fn main() {
par_0();
par_1(true);
par_3(12, 7.1, "cool");
par_0_cfg();
par_1_cfg(1);
par_0(<error descr="This function takes 0 parameters but 1 parameter was supplied [E0061]">4</error>);
par_1(<error descr="This function takes 1 parameter but 0 parameters were supplied [E0061]">)</error>;
par_1(true, <error descr="This function takes 1 parameter but 2 parameters were supplied [E0061]">false</error>);
par_3(5, 1.0<error descr="This function takes 3 parameters but 2 parameters were supplied [E0061]">)</error>;
par_0_cfg(<error descr="This function takes 0 parameters but 1 parameter was supplied [E0061]">4</error>);
par_1_cfg(<error descr="This function takes 1 parameter but 0 parameters were supplied [E0061]">)</error>;
}
""")
fun `test invalid parameters number in assoc function E0061`() = checkErrors("""
struct Foo;
impl Foo {
fn par_0() {}
fn par_2(p1: u32, p2: f64) {}
}
fn main() {
Foo::par_0();
Foo::par_2(12, 7.1);
Foo::par_0(<error descr="This function takes 0 parameters but 1 parameter was supplied [E0061]">4</error>);
Foo::par_2(5, 1.0, <error descr="This function takes 2 parameters but 3 parameters were supplied [E0061]">"three"</error>);
}
""")
fun `test invalid parameters number in impl methods E0061`() = checkErrors("""
struct Foo;
impl Foo {
fn par_0(&self) {}
fn par_2(&self, p1: u32, p2: f64) {}
}
fn main() {
let foo = Foo;
foo.par_0();
foo.par_2(12, 7.1);
foo.par_0(<error descr="This function takes 0 parameters but 1 parameter was supplied [E0061]">4</error>);
foo.par_2(5, 1.0, <error descr="This function takes 2 parameters but 3 parameters were supplied [E0061]">"three"</error>);
foo.par_2(<error descr="This function takes 2 parameters but 0 parameters were supplied [E0061]">)</error>;
}
""")
fun `test invalid parameters number in tuple structs E0061`() = checkErrors("""
struct Foo0();
struct Foo1(u8);
fn main() {
let _ = Foo0();
let _ = Foo1(1);
let _ = Foo0(<error descr="This function takes 0 parameters but 1 parameter was supplied [E0061]">4</error>);
let _ = Foo1(10, <error descr="This function takes 1 parameter but 2 parameters were supplied [E0061]">false</error>);
}
""")
fun `test invalid parameters number in tuple enum variants E0061`() = checkErrors("""
enum Foo {
VAR0(),
VAR1(u8)
}
fn main() {
let _ = Foo::VAR0();
let _ = Foo::VAR1(1);
let _ = Foo::VAR0(<error descr="This function takes 0 parameters but 1 parameter was supplied [E0061]">4</error>);
let _ = Foo::VAR1(10, <error descr="This function takes 1 parameter but 2 parameters were supplied [E0061]">false</error>);
}
""")
fun `test respects cfg attribute E0061`() = checkErrors("""
struct Foo;
impl Foo {
#[cfg(windows)]
fn bar(&self, p1: u32) {}
#[cfg(not(windows))]
fn bar(&self) {}
}
fn main() {
let foo = Foo;
foo.bar(<error descr="This function takes 0 parameters but 1 parameter was supplied [E0061]">10</error>);
foo.bar();
}
""")
// We would like to cover such cases, but the resolve engine has some flaws at the moment,
// so just ignore trait implementations to remove false positives
fun `test ignores trait implementations E0061`() = checkErrors("""
trait Foo1 { fn foo(&self); }
trait Foo2 { fn foo(&self, a: u8); }
struct Bar;
impl Foo1 for Bar {
fn foo(&self) {}
}
impl<T> Foo2 for Box<T> {
fn foo(&self, a: u8) {}
}
type BFoo1<'a> = Box<Foo1 + 'a>;
fn main() {
let bar: BFoo1 = Box::new(Bar);
bar.foo(); // Resolves to Foo2.foo() for Box<T>, though Foo1.foo() for Bar is the correct one
}
""")
fun `test type-qualified UFCS path E0061`() = checkErrors("""
struct S;
impl S { fn foo(self) { } }
fn main() {
<S>::foo(S);
}
""")
fun `test empty return E0069`() = checkErrors("""
fn ok1() { return; }
fn ok2() -> () { return; }
fn ok3() -> u32 {
let _ = || return;
return 10
}
fn ok4() -> u32 {
let _ = async { return; };
return 10
}
fn err1() -> bool {
<error descr="`return;` in a function whose return type is not `()` [E0069]">return</error>;
}
fn err2() -> ! {
<error>return</error>
}
""")
@MockRustcVersion("1.33.0-nightly")
fun `test type placeholder in signatures E0121`() = checkErrors("""
fn ok(_: &'static str) {
let four = |x: _| 4;
let _ = match (8, 3) { (_, _) => four(1) };
if let Some(_) = Some(0) {}
let foo = || -> _ { 42 };
let bar = || -> Option<_> { Some(1) };
let xy : Vec<(&str, fn(_, _) -> _)> = vec![
("x", |a: i32, b: i32| a + b),
("y", |a: i32, b: i32| a - b)
];
}
fn foo(a: <error descr="The type placeholder `_` is not allowed within types on item signatures [E0121]">_</error>) {}
fn bar() -> <error>_</error> {}
fn baz(t: (u32, <error>_</error>)) -> (bool, (f64, <error>_</error>)) {}
static FOO: <error>_</error> = 42;
""")
fun `test name duplication in struct E0124`() = checkErrors("""
struct S {
no_dup: bool,
<error descr="Field `dup` is already declared [E0124]">dup</error>: f64,
<error descr="Field `dup` is already declared [E0124]">dup</error>: f64
}
enum E {
VAR1 {
no_dup: bool
},
VAR2 {
no_dup: bool,
<error descr="Field `dup` is already declared [E0124]">dup</error>: f64,
<error descr="Field `dup` is already declared [E0124]">dup</error>: f64
}
}
""")
fun `test negative impls for traits E0198`() = checkErrors("""
struct Foo;
struct Foo2;
trait Bar1 { }
unsafe trait Bar2 { }
impl !Bar1 for Foo { }
impl !Bar2 for Foo { }
unsafe impl !<error descr="Negative implementations are not unsafe [E0198]">Bar1</error> for Foo2 { }
unsafe impl !<error descr="Negative implementations are not unsafe [E0198]">Bar2</error> for Foo2 { }
""")
fun `test only safe impls for safe traits E0199`() = checkErrors("""
struct Foo;
struct Foo2;
trait Bar { }
unsafe impl <error descr="Implementing the trait `Bar` is not unsafe [E0199]">Bar</error> for Foo { }
impl Bar for Foo2 { }
""")
fun `test only unsafe impls for unsafe traits E0200`() = checkErrors("""
struct Foo;
struct Foo2;
unsafe trait Bar { }
unsafe impl Bar for Foo { }
impl <error descr="The trait `Bar` requires an `unsafe impl` declaration [E0200]">Bar</error> for Foo2 { }
""")
fun `test may_dangle E0569`() = checkErrors("""
struct Foo1<T>(T);
struct Foo2<T>(T);
struct Foo3<T>(T);
struct Foo4<T>(T);
trait Bar { }
unsafe impl<#[may_dangle] A> Bar for Foo1<A> { }
impl<#[may_dangle] A> <error descr="Requires an `unsafe impl` declaration due to `#[may_dangle]` attribute [E0569]">Bar</error> for Foo2<A> { }
unsafe impl<#[may_dangle]'a, A> Bar for Foo3<A> { }
impl<#[may_dangle]'a, A> <error descr="Requires an `unsafe impl` declaration due to `#[may_dangle]` attribute [E0569]">Bar</error> for Foo4<A> { }
""")
fun `test name duplication in impl E0201`() = checkErrors("""
struct Foo;
impl Foo {
fn fn_unique() {}
fn <error descr="Duplicate definitions with name `dup` [E0201]">dup</error>(&self, a: u32) {}
fn <error descr="Duplicate definitions with name `dup` [E0201]">dup</error>(&self, a: u32) {}
}
trait Bar {
const UNIQUE: u32;
const TRAIT_DUP: u32;
fn unique() {}
fn trait_dup() {}
}
impl Bar for Foo {
const UNIQUE: u32 = 14;
const <error descr="Duplicate definitions with name `TRAIT_DUP` [E0201]">TRAIT_DUP</error>: u32 = 101;
const <error descr="Duplicate definitions with name `TRAIT_DUP` [E0201]">TRAIT_DUP</error>: u32 = 101;
fn unique() {}
fn <error descr="Duplicate definitions with name `trait_dup` [E0201]">trait_dup</error>() {}
fn <error descr="Duplicate definitions with name `trait_dup` [E0201]">trait_dup</error>() {}
}
""")
fun `test undeclared lifetimes E0261`() = checkErrors("""
fn foo<'a, 'b>(x: &'a u32, f: &'b Fn(&'b u8) -> &'b str) -> &'a u32 { x }
const FOO: for<'a> fn(&'a u32) -> &'a u32 = foo_func;
struct Struct<'a> { s: &'a str }
enum En<'a, 'b> { A(&'a u32), B(&'b bool) }
type Str<'d> = &'d str;
fn foo_err<'a>(x: &<error descr="Use of undeclared lifetime name `'b` [E0261]">'b</error> str) {}
fn bar() {
'foo: loop {
let _: &<error descr="Use of undeclared lifetime name `'foo` [E0261]">'foo</error> str;
}
}
""")
fun `test not applied to static lifetimes E0261`() = checkErrors("""
const ZERO: &'static u32 = &0;
fn foo(a: &'static str) {}
""")
fun `test not applied to underscore lifetimes E0261`() = checkErrors("""
const ZERO: &'_ u32 = &0;
fn foo(a: &'_ str) {}
""")
fun `test reserved lifetime name ('static) E0262`() = checkErrors("""
fn foo1<<error descr="`'static` is a reserved lifetime name [E0262]">'static</error>>(x: &'static str) {}
struct Str1<<error>'static</error>> { a: &'static u32 }
impl<<error>'static</error>> Str1<'static> {}
enum En1<<error>'static</error>> { A(&'static str) }
trait Tr1<<error>'static</error>> {}
""")
fun `test reserved lifetime name ('_) E0262`() = checkErrors("""
fn foo2<<error descr="`'_` is a reserved lifetime name [E0262]">'_</error>>(x: &'_ str) {}
struct Str2<<error>'_</error>> { a: &'_ u32 }
impl<<error>'_</error>> Str2<'_> {}
enum En2<<error>'_</error>> { A(&'_ str) }
trait Tr2<<error>'_</error>> {}
""")
@MockRustcVersion("1.23.0")
fun `test in-band lifetimes feature E0658 1`() = checkErrors("""
fn foo(x: &<error descr="in-band lifetimes is experimental [E0658]">'a</error> str) {}
""")
@MockRustcVersion("1.23.0-nightly")
fun `test in-band lifetimes feature E0658 2`() = checkErrors("""
#![feature(in_band_lifetimes)]
fn foo<T: 'a>(x: &'b str) where 'c: 'd {}
""")
@MockRustcVersion("1.23.0-nightly")
fun `test in-band lifetimes feature E0658 3`() = checkErrors("""
#![feature(in_band_lifetimes)]
fn foo<'b>(x: &<error descr="Cannot mix in-band and explicit lifetime definitions [E0688]">'a</error> str) {}
""")
@MockRustcVersion("1.23.0-nightly")
fun `test in-band lifetimes feature E0658 4`() = checkErrors("""
#![feature(in_band_lifetimes)]
fn foo() {
let x: &<error descr="Use of undeclared lifetime name `'a` [E0261]">'a</error> str = unimplemented!();
}
""")
fun `test lifetime name duplication in generic params E0263`() = checkErrors("""
fn foo<'a, 'b>(x: &'a str, y: &'b str) { }
struct Str<'a, 'b> { a: &'a u32, b: &'b f64 }
impl<'a, 'b> Str<'a, 'b> {}
enum Direction<'a, 'b> { LEFT(&'a str), RIGHT(&'b str) }
trait Trait<'a, 'b> {}
fn bar<<error descr="Lifetime name `'a` declared twice in the same scope [E0263]">'a</error>, 'b, <error>'a</error>>(x: &'a str, y: &'b str) { }
struct St<<error>'a</error>, 'b, <error>'a</error>> { a: &'a u32, b: &'b f64 }
impl<<error>'a</error>, 'b, <error>'a</error>> Str<'a, 'b> {}
enum Dir<<error>'a</error>, 'b, <error>'a</error>> { LEFT(&'a str), RIGHT(&'b str) }
trait Tr<<error>'a</error>, 'b, <error>'a</error>> {}
""")
@MockRustcVersion("1.34.0-nightly")
fun `test name duplication in generic params E0403`() = checkErrors("""
#![feature(const_generics)]
fn f1<T1, T2>() {}
fn f2<T1, const T2: i32>() {}
fn f3<const T1: i32, const T2: i32>() {}
fn f4<<error descr="The name `T1` is already used for a type parameter in this type parameter list [E0403]">T1</error>, <error>T2</error>, T3, <error>T1</error>, const <error>T4</error>: i32, const T5: i32, const <error>T4</error>: i32, const <error>T2</error>: i32>() {}
struct S1<T1, T2> { t1: T1, t2: T2 }
struct S2<T1, const T2: i32> { t1: T1 }
struct S3<const T1: i32, const T2: i32> {}
struct S4<<error>T1</error>, <error>T2</error>, T3, <error>T1</error>, const <error>T4</error>: i32, const T5: i32, const <error>T4</error>: i32, const <error>T2</error>: i32> { t: T1, p: T2 }
impl<T1, T2> S1<T1, T2> {}
impl<<error>T1</error>, T2, T3, <error>T1</error>> S4<T1, T2, T3, T1, 0, 0, 0, 0> {}
enum E1<T1, T2> { L(T1), R(T2) }
enum E2<T1, const T2: i32> { L(T1) }
enum E3<const T1: i32, const T2: i32> {}
enum E4<<error>T1</error>, <error>T2</error>, T3, <error>T1</error>, const <error>T4</error>: i32, const T5: i32, const <error>T4</error>: i32, const <error>T2</error>: i32> { L(T1), M(T2), R(T3) }
trait Tr1<T1, T2> {}
trait Tr2<T1, const T2: i32> {}
trait Tr3<const T1: i32, const T2: i32> {}
trait Tr4<<error>T1</error>, <error>T2</error>, T3, <error>T1</error>, const <error>T4</error>: i32, const T5: i32, const <error>T4</error>: i32, const <error>T2</error>: i32> {}
""")
fun `test no E0407 for method defined with a macro`() = checkErrors("""
macro_rules! foo {
($ i:ident, $ j:ty) => { fn $ i(&self) -> $ j { unimplemented!() } }
}
trait T {
foo!(foo, ());
}
impl T for () {
fn foo(&self) {}
}
""")
@MockAdditionalCfgOptions("intellij_rust")
fun `test name duplication in param list E0415`() = checkErrors("""
fn foo(x: u32, X: u32) {}
fn bar<T>(T: T) {}
fn simple(<error descr="Identifier `a` is bound more than once in this parameter list [E0415]">a</error>: u32,
b: bool,
<error>a</error>: f64) {}
fn tuples(<error>a</error>: u8, (b, (<error>a</error>, c)): (u16, (u32, u64))) {}
fn fn_ptrs(x: i32, y: fn (x: i32, y: i32), z: fn (x: i32, x: i32)) {}
fn cfg_1(#[cfg(intellij_rust)] a: i32, #[cfg(not(intellij_rust))] a: u32) {}
fn cfg_2(#[cfg(intellij_rust)] <error>a</error>: i32, #[cfg(intellij_rust)] <error>a</error>: u32) {}
fn cfg_3(#[cfg(intellij_rust)] <error>a</error>: i32, <error>a</error>: u32) {}
trait Foo {
fn foo(&self,
<error descr="Identifier `x` is bound more than once in this parameter list [E0415]">x</error>: i32,
<error descr="Identifier `x` is bound more than once in this parameter list [E0415]">x</error>: i32
) {}
}
""")
fun `test name duplication in param list of non-default trait methods`() = checkErrors("""
trait Foo {
fn bar(x: i32, x: i32);
}
""")
fun `test undeclared label E0426`() = checkErrors("""
fn ok() {
'foo: loop { break 'foo }
'bar: while true { continue 'bar }
'baz: for _ in 0..3 { break 'baz }
'outer: loop {
'inner: while true { break 'outer }
}
}
fn err<'a>(a: &'a str) {
'foo: loop { continue <error descr="Use of undeclared label `'bar` [E0426]">'bar</error> }
while true { break <error descr="Use of undeclared label `'static` [E0426]">'static</error> }
for _ in 0..1 { break <error descr="Use of undeclared label `'a` [E0426]">'a</error> }
}
""")
fun `test name duplication in code block E0428`() = checkErrors("""
fn abc() {
const UNIQUE_CONST: i32 = 10;
static UNIQUE_STATIC: f64 = 0.72;
fn unique_fn() {}
struct UniqueStruct;
trait UniqueTrait {}
enum UniqueEnum {}
mod unique_mod {}
const <error descr="A value named `Dup` has already been defined in this block [E0428]">Dup</error>: u32 = 20;
static <error descr="A value named `Dup` has already been defined in this block [E0428]">Dup</error>: i64 = -1.3;
fn <error descr="A value named `Dup` has already been defined in this block [E0428]">Dup</error>() {}
struct <error descr="A type named `Dup` has already been defined in this block [E0428]">Dup</error>;
trait <error descr="A type named `Dup` has already been defined in this block [E0428]">Dup</error> {}
enum <error descr="A type named `Dup` has already been defined in this block [E0428]">Dup</error> {}
mod <error descr="A type named `Dup` has already been defined in this block [E0428]">Dup</error> {}
}
""")
fun `test name duplication in enum E0428`() = checkErrors("""
enum Directions {
NORTH,
<error descr="Enum variant `SOUTH` is already declared [E0428]">SOUTH</error> { distance: f64 },
WEST,
<error descr="Enum variant `SOUTH` is already declared [E0428]">SOUTH</error> { distance: f64 },
EAST
}
""")
fun `test name duplication in foreign mod E0428`() = checkErrors("""
extern "C" {
static mut UNIQUE: u16;
fn unique();
static mut <error descr="A value named `DUP` has already been defined in this module [E0428]">DUP</error>: u32;
static mut <error descr="A value named `DUP` has already been defined in this module [E0428]">DUP</error>: u32;
fn <error descr="A value named `dup` has already been defined in this module [E0428]">dup</error>();
fn <error descr="A value named `dup` has already been defined in this module [E0428]">dup</error>();
}
""")
fun `test name duplication in file E0428`() = checkErrors("""
const UNIQUE_CONST: i32 = 10;
static UNIQUE_STATIC: f64 = 0.72;
fn unique_fn() {}
struct UniqueStruct;
trait UniqueTrait {}
enum UniqueEnum {}
mod unique_mod {}
const <error descr="A value named `Dup` has already been defined in this module [E0428]">Dup</error>: u32 = 20;
static <error descr="A value named `Dup` has already been defined in this module [E0428]">Dup</error>: i64 = -1.3;
fn <error descr="A value named `Dup` has already been defined in this module [E0428]">Dup</error>() {}
struct <error descr="A type named `Dup` has already been defined in this module [E0428]">Dup</error>;
trait <error descr="A type named `Dup` has already been defined in this module [E0428]">Dup</error> {}
enum <error descr="A type named `Dup` has already been defined in this module [E0428]">Dup</error> {}
mod <error descr="A type named `Dup` has already been defined in this module [E0428]">Dup</error> {}
""")
fun `test name duplication in module E0428`() = checkErrors("""
mod foo {
const UNIQUE_CONST: i32 = 10;
static UNIQUE_STATIC: f64 = 0.72;
fn unique_fn() {}
struct UniqueStruct;
trait UniqueTrait {}
enum UniqueEnum {}
mod unique_mod {}
const <error descr="A value named `Dup` has already been defined in this module [E0428]">Dup</error>: u32 = 20;
static <error descr="A value named `Dup` has already been defined in this module [E0428]">Dup</error>: i64 = -1.3;
fn <error descr="A value named `Dup` has already been defined in this module [E0428]">Dup</error>() {}
struct <error descr="A type named `Dup` has already been defined in this module [E0428]">Dup</error>;
trait <error descr="A type named `Dup` has already been defined in this module [E0428]">Dup</error> {}
enum <error descr="A type named `Dup` has already been defined in this module [E0428]">Dup</error> {}
mod <error descr="A type named `Dup` has already been defined in this module [E0428]">Dup</error> {}
}
""")
fun `test name duplication in trait E0428`() = checkErrors("""
trait T {
type NO_DUP_T;
const NO_DUP_C: u8;
fn no_dup_f();
type <error descr="A type named `DUP_T` has already been defined in this trait [E0428]">DUP_T</error>;
type <error descr="A type named `DUP_T` has already been defined in this trait [E0428]">DUP_T</error>;
const <error descr="A value named `DUP_C` has already been defined in this trait [E0428]">DUP_C</error>: u32;
const <error descr="A value named `DUP_C` has already been defined in this trait [E0428]">DUP_C</error>: u32;
fn <error descr="A value named `dup` has already been defined in this trait [E0428]">dup</error>(&self);
fn <error descr="A value named `dup` has already been defined in this trait [E0428]">dup</error>(&self);
}
""")
fun `test respects namespaces E0428`() = checkErrors("""
mod m {
// Consts and types are in different namespaces
type NO_C_DUP = bool;
const NO_C_DUP: u32 = 10;
// Functions and types are in different namespaces
type NO_F_DUP = u8;
fn NO_F_DUP() {}
// Consts and functions are in the same namespace (values)
fn <error descr="A value named `DUP_V` has already been defined in this module [E0428]">DUP_V</error>() {}
const <error>DUP_V</error>: u8 = 1;
// Enums and traits are in the same namespace (types)
trait <error descr="A type named `DUP_T` has already been defined in this module [E0428]">DUP_T</error> {}
enum <error>DUP_T</error> {}
mod <error>foo</error>;
fn foo() {}
// Macros have thair own namespace
type NO_M_DUP = u16;
macro NO_M_DUP(){}
}
""")
fun `test ignores local bindings E0428`() = checkErrors("""
mod no_dup {
fn no_dup() {
let no_dup: bool = false;
fn no_dup(no_dup: u23) {
mod no_dup {}
}
}
}
""")
fun `test ignores inner containers E0428`() = checkErrors("""
mod foo {
const NO_DUP: u8 = 4;
fn f() {
const NO_DUP: u8 = 7;
{ const NO_DUP: u8 = 9; }
}
struct S { NO_DUP: u8 }
trait T { const NO_DUP: u8 = 3; }
enum E { NO_DUP }
mod m { const NO_DUP: u8 = 1; }
}
""")
@MockAdditionalCfgOptions("intellij_rust")
fun `test respects cfg attribute E0428`() = checkErrors("""
mod opt {
#[cfg(not(bar))] mod foo {}
#[cfg(bar)] mod foo {}
#[cfg(intellij_rust)] fn <error descr="A value named `hello_world` has already been defined in this module [E0428]">hello_world</error>() {}
fn <error descr="A value named `hello_world` has already been defined in this module [E0428]">hello_world</error>() {}
#[cfg(bar)] fn hello_rust() {}
fn hello_rust() {}
}
""")
fun `test macro mod fn no E0428`() = checkErrors("""
macro_rules! example {
() => ()
}
mod example { }
fn example() { }
""")
fun `test macro struct no E0428`() = checkErrors("""
macro_rules! example {
() => ()
}
struct example { }
""")
fun `test duplicate macro no E0428`() = checkErrors("""
macro_rules! example {
() => ()
}
macro_rules! example {
() => ()
}
""")
fun `test duplicates with import E0252`() = checkErrors("""
use bar::{<error descr="A second item with name 'test1' imported. Try to use an alias. [E0252]">test1</error>};
use baz::<error descr="A second item with name 'test2' imported. Try to use an alias. [E0252]">test2</error>;
use bar::<error descr="A second item with name 'test3' imported. Try to use an alias. [E0252]">test3</error>;
use baz::<error descr="A second item with name 'test3' imported. Try to use an alias. [E0252]">test3</error>;
use bar::A as <error descr="A second item with name 'Arc' imported. Try to use an alias. [E0252]">Arc</error>;
struct <error descr="A type named `Arc` has already been defined in this module [E0428]">Arc</error>{}
fn <error descr="A value named `test1` has already been defined in this module [E0428]">test1</error>(){}
fn <error descr="A value named `test2` has already been defined in this module [E0428]">test2</error>(){}
mod bar{
pub struct A{}
pub mod test3{}
pub fn test1(){}
}
mod baz{
pub struct test3{}
pub const test2:u8 = 0;
}
""")
fun `test no duplicates with import E0252`() = checkErrors("""
use bar::{test1};
use baz::test2;
use bar::test3;
use bar::unresolved;
use baz::unresolved;
use baz::test3;
use bar::Arc as A;
struct Arc{}
fn test1(){}
fn test2(){}
mod bar{
pub struct Arc{}
pub mod test1{}
pub mod test3{}
}
mod baz{
pub struct test2{}
pub const test3:u8 = 0;
}
""")
fun `test unnecessary pub E0449`() = checkErrors("""
<error descr="Unnecessary visibility qualifier [E0449]">pub</error> extern "C" { }
pub struct S {
foo: bool,
pub bar: u8,
pub baz: (u32, f64)
}
<error>pub</error> impl S {}
struct STuple (pub u32, f64);
pub enum E {
FOO {
bar: u32,
<error>pub</error> baz: u32
},
<error>pub</error> BAR(<error>pub</error> u32, f64),
<error>pub(crate)</error> BAZ
}
pub trait Foo {
type A;
fn b();
const C: u32;
}
struct Bar;
<error>pub</error> impl Foo for Bar {
<error>pub</error> type A = u32;
<error>pub</error> fn b() {}
<error>pub</error> const C: u32 = 10;
}
""")
fun `test self in static method E0424`() = checkErrors("""
struct Foo;
impl Foo {
fn foo() {
let a = <error descr="The self keyword was used in a static method [E0424]">self</error>;
}
}
""")
fun `test self in vis restriction in static method no E0424`() = checkErrors("""
struct Foo;
impl Foo {
pub(self) fn foo() {}
}
""")
fun `test self expression outside function`() = checkErrors("""
const C: () = <error descr="self value is not available in this context">self</error>;
""")
fun `test do not annotate 'self' in visibility restriction`() = checkErrors("""
struct Foo {
pub(self) attr1: bool,
pub(in self) attr2: bool
}
""")
fun `test ignore non static E0424`() = checkErrors("""
struct Foo;
impl Foo {
fn foo(self) {
let a = self;
}
}
""")
fun `test ignore module path E0424`() = checkErrors("""
fn foo() {
}
fn bar() {
self::foo()
}
""")
fun `test ignore use self with parens E0424`() = checkErrors("""
fn foo() {}
fn bat() {}
fn bar() {
use self::{foo};
use self::{foo,bat};