Skip to content

Commit ab52ba2

Browse files
committed
Fix unused_variables in tests
1 parent 7834b8f commit ab52ba2

13 files changed

+122
-104
lines changed

crates/hir-ty/src/mir/lower.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1774,13 +1774,14 @@ impl<'ctx> MirLowerCtx<'ctx> {
17741774
}
17751775
}
17761776
}
1777-
hir_def::hir::Statement::Expr { expr, has_semi: _ } => {
1777+
&hir_def::hir::Statement::Expr { expr, has_semi: _ } => {
17781778
let scope2 = self.push_drop_scope();
1779-
let Some((_, c)) = self.lower_expr_as_place(current, *expr, true)? else {
1779+
let Some((p, c)) = self.lower_expr_as_place(current, expr, true)? else {
17801780
scope2.pop_assume_dropped(self);
17811781
scope.pop_assume_dropped(self);
17821782
return Ok(None);
17831783
};
1784+
self.push_fake_read(c, p, expr.into());
17841785
current = scope2.pop_and_drop(self, c);
17851786
}
17861787
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn main() {
166166
check_diagnostics(
167167
r#"
168168
struct A { a: &'static str }
169-
fn f(a: A) { let A { a: hello } = a; }
169+
fn f(a: A) { let A { a: _hello } = a; }
170170
"#,
171171
);
172172
check_diagnostics(
@@ -181,12 +181,14 @@ fn f(a: A) { let A { 0: 0 } = a; }
181181
struct A { a: &'static str }
182182
fn f(a: A) {
183183
let A { a$0: a } = a;
184+
_ = a;
184185
}
185186
"#,
186187
r#"
187188
struct A { a: &'static str }
188189
fn f(a: A) {
189190
let A { a } = a;
191+
_ = a;
190192
}
191193
"#,
192194
);
@@ -196,12 +198,14 @@ fn f(a: A) {
196198
struct A { a: &'static str, b: &'static str }
197199
fn f(a: A) {
198200
let A { a$0: a, b } = a;
201+
_ = (a, b);
199202
}
200203
"#,
201204
r#"
202205
struct A { a: &'static str, b: &'static str }
203206
fn f(a: A) {
204207
let A { a, b } = a;
208+
_ = (a, b);
205209
}
206210
"#,
207211
);

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ fn NonSnakeCaseName() {}
175175
fn incorrect_function_params() {
176176
check_diagnostics(
177177
r#"
178-
fn foo(SomeParam: u8) {}
178+
fn foo(SomeParam: u8) { _ = SomeParam; }
179179
// ^^^^^^^^^ 💡 warn: Parameter `SomeParam` should have snake_case name, e.g. `some_param`
180180
181-
fn foo2(ok_param: &str, CAPS_PARAM: u8) {}
181+
fn foo2(ok_param: &str, CAPS_PARAM: u8) { _ = (ok_param, CAPS_PARAM); }
182182
// ^^^^^^^^^^ 💡 warn: Parameter `CAPS_PARAM` should have snake_case name, e.g. `caps_param`
183183
"#,
184184
);
@@ -188,6 +188,7 @@ fn foo2(ok_param: &str, CAPS_PARAM: u8) {}
188188
fn incorrect_variable_names() {
189189
check_diagnostics(
190190
r#"
191+
#[allow(unused)]
191192
fn foo() {
192193
let SOME_VALUE = 10;
193194
// ^^^^^^^^^^ 💡 warn: Variable `SOME_VALUE` should have snake_case name, e.g. `some_value`
@@ -294,6 +295,7 @@ impl someStruct {
294295
// ^^^^^^^^ 💡 warn: Function `SomeFunc` should have snake_case name, e.g. `some_func`
295296
let WHY_VAR_IS_CAPS = 10;
296297
// ^^^^^^^^^^^^^^^ 💡 warn: Variable `WHY_VAR_IS_CAPS` should have snake_case name, e.g. `why_var_is_caps`
298+
_ = WHY_VAR_IS_CAPS;
297299
}
298300
}
299301
"#,
@@ -306,6 +308,7 @@ impl someStruct {
306308
r#"
307309
enum Option { Some, None }
308310
311+
#[allow(unused)]
309312
fn main() {
310313
match Option::None {
311314
None => (),
@@ -322,6 +325,7 @@ fn main() {
322325
r#"
323326
enum Option { Some, None }
324327
328+
#[allow(unused)]
325329
fn main() {
326330
match Option::None {
327331
SOME_VAR @ None => (),
@@ -349,7 +353,9 @@ enum E {
349353
}
350354
351355
mod F {
352-
fn CheckItWorksWithCrateAttr(BAD_NAME_HI: u8) {}
356+
fn CheckItWorksWithCrateAttr(BAD_NAME_HI: u8) {
357+
_ = BAD_NAME_HI;
358+
}
353359
}
354360
"#,
355361
);
@@ -395,7 +401,7 @@ fn qualify() {
395401

396402
#[test] // Issue #8809.
397403
fn parenthesized_parameter() {
398-
check_diagnostics(r#"fn f((O): _) {}"#)
404+
check_diagnostics(r#"fn f((O): _) { _ = O; }"#)
399405
}
400406

401407
#[test]
@@ -472,7 +478,9 @@ mod CheckBadStyle {
472478
473479
mod F {
474480
#![allow(non_snake_case)]
475-
fn CheckItWorksWithModAttr(BAD_NAME_HI: u8) {}
481+
fn CheckItWorksWithModAttr(BAD_NAME_HI: u8) {
482+
_ = BAD_NAME_HI;
483+
}
476484
}
477485
478486
#[allow(non_snake_case, non_camel_case_types)]

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ fn f() { zero(); }
131131
fn simple_free_fn_one() {
132132
check_diagnostics(
133133
r#"
134-
fn one(arg: u8) {}
134+
fn one(_arg: u8) {}
135135
fn f() { one(); }
136136
//^^ error: expected 1 argument, found 0
137137
"#,
138138
);
139139

140140
check_diagnostics(
141141
r#"
142-
fn one(arg: u8) {}
142+
fn one(_arg: u8) {}
143143
fn f() { one(1); }
144144
"#,
145145
);
@@ -176,7 +176,7 @@ fn f() {
176176
check_diagnostics(
177177
r#"
178178
struct S;
179-
impl S { fn method(&self, arg: u8) {} }
179+
impl S { fn method(&self, _arg: u8) {} }
180180
181181
fn f() {
182182
S.method();
@@ -187,7 +187,7 @@ impl S { fn method(&self, arg: u8) {} }
187187
check_diagnostics(
188188
r#"
189189
struct S;
190-
impl S { fn method(&self, arg: u8) {} }
190+
impl S { fn method(&self, _arg: u8) {} }
191191
192192
fn f() {
193193
S::method(&S, 0);
@@ -335,8 +335,8 @@ struct S;
335335
336336
impl S {
337337
fn method(#[cfg(NEVER)] self) {}
338-
fn method2(#[cfg(NEVER)] self, arg: u8) {}
339-
fn method3(self, #[cfg(NEVER)] arg: u8) {}
338+
fn method2(#[cfg(NEVER)] self, _arg: u8) {}
339+
fn method3(self, #[cfg(NEVER)] _arg: u8) {}
340340
}
341341
342342
extern "C" {
@@ -365,8 +365,8 @@ fn main() {
365365
r#"
366366
#[rustc_legacy_const_generics(1, 3)]
367367
fn mixed<const N1: &'static str, const N2: bool>(
368-
a: u8,
369-
b: i8,
368+
_a: u8,
369+
_b: i8,
370370
) {}
371371
372372
fn f() {
@@ -376,8 +376,8 @@ fn f() {
376376
377377
#[rustc_legacy_const_generics(1, 3)]
378378
fn b<const N1: u8, const N2: u8>(
379-
a: u8,
380-
b: u8,
379+
_a: u8,
380+
_b: u8,
381381
) {}
382382
383383
fn g() {
@@ -403,7 +403,7 @@ fn f(
403403
// ^^ error: this pattern has 0 fields, but the corresponding tuple struct has 2 fields
404404
S(e, f, .., g, d): S
405405
// ^^^^^^^^^ error: this pattern has 4 fields, but the corresponding tuple struct has 2 fields
406-
) {}
406+
) { _ = (a, b, c, d, e, f, g); }
407407
"#,
408408
)
409409
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ fn x(a: S) {
290290
struct S { s: u32 }
291291
fn x(a: S) {
292292
let S { ref s } = a;
293+
_ = s;
293294
}
294295
",
295296
)
@@ -626,7 +627,7 @@ struct TestStruct { one: i32, two: i64 }
626627
627628
fn test_fn() {
628629
let one = 1;
629-
let s = TestStruct{ one, two: 2 };
630+
let _s = TestStruct{ one, two: 2 };
630631
}
631632
"#,
632633
);

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(crate) fn missing_match_arms(
1919
mod tests {
2020
use crate::tests::check_diagnostics;
2121

22+
#[track_caller]
2223
fn check_diagnostics_no_bails(ra_fixture: &str) {
2324
cov_mark::check_count!(validate_match_bailed_out, 0);
2425
crate::tests::check_diagnostics(ra_fixture)
@@ -564,6 +565,7 @@ fn bang(never: !) {
564565
r#"
565566
enum Option<T> { Some(T), None }
566567
568+
#[allow(unused)]
567569
fn main() {
568570
// `Never` is deliberately not defined so that it's an uninferred type.
569571
match Option::<Never>::None {
@@ -719,7 +721,7 @@ fn main() {
719721
r#"
720722
struct S { a: char}
721723
fn main(v: S) {
722-
match v { S{ a } => {} }
724+
match v { S{ a } => { _ = a; } }
723725
match v { S{ a: _x } => {} }
724726
match v { S{ a: 'a' } => {} }
725727
match v { S{..} => {} }
@@ -901,7 +903,7 @@ enum E{ A, B }
901903
fn foo() {
902904
match &E::A {
903905
E::A => {}
904-
x => {}
906+
_x => {}
905907
}
906908
}",
907909
);

0 commit comments

Comments
 (0)