Rollup merge of #81473 - sanxiyn:write-only-field, r=oli-obk
Warn write-only fields cc `@Boscop's` example in #49256.
This commit is contained in:
commit
774ba83226
@ -37,6 +37,15 @@ fn should_explore(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
|
||||
)
|
||||
}
|
||||
|
||||
fn base_expr<'a>(mut expr: &'a hir::Expr<'a>) -> &'a hir::Expr<'a> {
|
||||
loop {
|
||||
match expr.kind {
|
||||
hir::ExprKind::Field(base, ..) => expr = base,
|
||||
_ => return expr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct MarkSymbolVisitor<'tcx> {
|
||||
worklist: Vec<hir::HirId>,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
@ -263,6 +272,12 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
|
||||
hir::ExprKind::MethodCall(..) => {
|
||||
self.lookup_and_handle_method(expr.hir_id);
|
||||
}
|
||||
hir::ExprKind::Assign(ref left, ref right, ..) => {
|
||||
// Ignore write to field
|
||||
self.visit_expr(base_expr(left));
|
||||
self.visit_expr(right);
|
||||
return;
|
||||
}
|
||||
hir::ExprKind::Field(ref lhs, ..) => {
|
||||
self.handle_field_access(&lhs, expr.hir_id);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
// run-pass
|
||||
// pretty-expanded FIXME #23616
|
||||
#![allow(dead_code)]
|
||||
|
||||
pub fn main() {
|
||||
struct A {
|
||||
|
20
src/test/ui/lint/dead-code/write-only-field.rs
Normal file
20
src/test/ui/lint/dead-code/write-only-field.rs
Normal file
@ -0,0 +1,20 @@
|
||||
#![deny(dead_code)]
|
||||
|
||||
struct S {
|
||||
f: i32, //~ ERROR: field is never read
|
||||
sub: Sub, //~ ERROR: field is never read
|
||||
}
|
||||
|
||||
struct Sub {
|
||||
f: i32, //~ ERROR: field is never read
|
||||
}
|
||||
|
||||
fn field_write(s: &mut S) {
|
||||
s.f = 1;
|
||||
s.sub.f = 2;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut s = S { f: 0, sub: Sub { f: 0 } };
|
||||
field_write(&mut s);
|
||||
}
|
26
src/test/ui/lint/dead-code/write-only-field.stderr
Normal file
26
src/test/ui/lint/dead-code/write-only-field.stderr
Normal file
@ -0,0 +1,26 @@
|
||||
error: field is never read: `f`
|
||||
--> $DIR/write-only-field.rs:4:5
|
||||
|
|
||||
LL | f: i32,
|
||||
| ^^^^^^
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/write-only-field.rs:1:9
|
||||
|
|
||||
LL | #![deny(dead_code)]
|
||||
| ^^^^^^^^^
|
||||
|
||||
error: field is never read: `sub`
|
||||
--> $DIR/write-only-field.rs:5:5
|
||||
|
|
||||
LL | sub: Sub,
|
||||
| ^^^^^^^^
|
||||
|
||||
error: field is never read: `f`
|
||||
--> $DIR/write-only-field.rs:9:5
|
||||
|
|
||||
LL | f: i32,
|
||||
| ^^^^^^
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
Loading…
x
Reference in New Issue
Block a user