Skip to content

Commit 562f4a2

Browse files
committed
test: update test for unused_variables
1 parent 7713657 commit 562f4a2

File tree

2 files changed

+87
-16
lines changed

2 files changed

+87
-16
lines changed

crates/ide-diagnostics/src/handlers/mutability_errors.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ fn main() {
413413
fn main() {
414414
return;
415415
let mut x = 2;
416-
//^^^^^ warn: unused variable
416+
//^^^^^ 💡 warn: unused variable
417417
&mut x;
418418
}
419419
"#,
@@ -423,7 +423,7 @@ fn main() {
423423
fn main() {
424424
loop {}
425425
let mut x = 2;
426-
//^^^^^ warn: unused variable
426+
//^^^^^ 💡 warn: unused variable
427427
&mut x;
428428
}
429429
"#,
@@ -444,7 +444,7 @@ fn main(b: bool) {
444444
g();
445445
}
446446
let mut x = 2;
447-
//^^^^^ warn: unused variable
447+
//^^^^^ 💡 warn: unused variable
448448
&mut x;
449449
}
450450
"#,
@@ -459,7 +459,7 @@ fn main(b: bool) {
459459
return;
460460
}
461461
let mut x = 2;
462-
//^^^^^ warn: unused variable
462+
//^^^^^ 💡 warn: unused variable
463463
&mut x;
464464
}
465465
"#,
@@ -789,7 +789,7 @@ fn f() {
789789
//^^ 💡 error: cannot mutate immutable variable `x`
790790
_ = (x, y);
791791
let x = Foo;
792-
//^ warn: unused variable
792+
//^ 💡 warn: unused variable
793793
let x = Foo;
794794
let y: &mut (i32, u8) = &mut x;
795795
//^^^^^^ 💡 error: cannot mutate immutable variable `x`

crates/ide-diagnostics/src/handlers/unused_variables.rs

+82-11
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn fixes(var_name: &String, diagnostic_range: FileRange, is_in_marco: bool) -> O
4747

4848
#[cfg(test)]
4949
mod tests {
50-
use crate::tests::check_diagnostics;
50+
use crate::tests::{check_diagnostics, check_fix, check_no_fix};
5151

5252
#[test]
5353
fn unused_variables_simple() {
@@ -57,31 +57,31 @@ mod tests {
5757
struct Foo { f1: i32, f2: i64 }
5858
5959
fn f(kkk: i32) {}
60-
//^^^ warn: unused variable
60+
//^^^ 💡 warn: unused variable
6161
fn main() {
6262
let a = 2;
63-
//^ warn: unused variable
63+
//^ 💡 warn: unused variable
6464
let b = 5;
6565
// note: `unused variable` implies `unused mut`, so we should not emit both at the same time.
6666
let mut c = f(b);
67-
//^^^^^ warn: unused variable
67+
//^^^^^ 💡 warn: unused variable
6868
let (d, e) = (3, 5);
69-
//^ warn: unused variable
69+
//^ 💡 warn: unused variable
7070
let _ = e;
7171
let f1 = 2;
7272
let f2 = 5;
7373
let f = Foo { f1, f2 };
7474
match f {
7575
Foo { f1, f2 } => {
76-
//^^ warn: unused variable
76+
//^^ 💡 warn: unused variable
7777
_ = f2;
7878
}
7979
}
8080
let g = false;
8181
if g {}
8282
let h: fn() -> i32 = || 2;
8383
let i = h();
84-
//^ warn: unused variable
84+
//^ 💡 warn: unused variable
8585
}
8686
"#,
8787
);
@@ -95,11 +95,11 @@ struct S {
9595
}
9696
impl S {
9797
fn owned_self(self, u: i32) {}
98-
//^ warn: unused variable
98+
//^ 💡 warn: unused variable
9999
fn ref_self(&self, u: i32) {}
100-
//^ warn: unused variable
100+
//^ 💡 warn: unused variable
101101
fn ref_mut_self(&mut self, u: i32) {}
102-
//^ warn: unused variable
102+
//^ 💡 warn: unused variable
103103
fn owned_mut_self(mut self) {}
104104
//^^^^^^^^ 💡 warn: variable does not need to be mutable
105105
@@ -131,7 +131,78 @@ fn main() {
131131
#[deny(unused)]
132132
fn main2() {
133133
let x = 2;
134-
//^ error: unused variable
134+
//^ 💡 error: unused variable
135+
}
136+
"#,
137+
);
138+
}
139+
140+
#[test]
141+
fn fix_unused_variable() {
142+
check_fix(
143+
r#"
144+
fn main() {
145+
let x$0 = 2;
146+
}
147+
"#,
148+
r#"
149+
fn main() {
150+
let _x = 2;
151+
}
152+
"#,
153+
);
154+
155+
check_fix(
156+
r#"
157+
fn main() {
158+
let ($0d, _e) = (3, 5);
159+
}
160+
"#,
161+
r#"
162+
fn main() {
163+
let (_d, _e) = (3, 5);
164+
}
165+
"#,
166+
);
167+
168+
check_fix(
169+
r#"
170+
struct Foo { f1: i32, f2: i64 }
171+
fn main() {
172+
let f = Foo { f1: 0, f2: 0 };
173+
match f {
174+
Foo { f1$0, f2 } => {
175+
_ = f2;
176+
}
177+
}
178+
}
179+
"#,
180+
r#"
181+
struct Foo { f1: i32, f2: i64 }
182+
fn main() {
183+
let f = Foo { f1: 0, f2: 0 };
184+
match f {
185+
Foo { _f1, f2 } => {
186+
_ = f2;
187+
}
188+
}
189+
}
190+
"#,
191+
);
192+
}
193+
194+
#[test]
195+
fn no_fix_for_marco() {
196+
check_no_fix(
197+
r#"
198+
macro_rules! my_macro {
199+
() => {
200+
let x = 3;
201+
};
202+
}
203+
204+
fn main() {
205+
$0my_macro!();
135206
}
136207
"#,
137208
);

0 commit comments

Comments
 (0)