Skip to content

False positive "field is never read" warnings #81626

Closed

Description

@emilio

Here's a reduced test-case from Firefox source code:

struct Data {
    pub field: bool,
}

struct Foo<'a> {
    data: &'a mut Data,
}

impl<'a> Foo<'a> {
    fn parse_data(&mut self) {
        macro_rules! parse {
            () => {
                self.data.field = true;
            }
        }
        parse!()
    }
}

fn main() {
    let mut data = Data { field: false };
    {
        let mut foo = Foo { data: &mut data };
        foo.parse_data();
    }
    println!("{}", data.field);
}

Compiling that program warns with:

warning: field is never read: `data`
 --> t.rs:6:5
  |
6 |     data: &'a mut Data,
  |     ^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

Which is obviously not true.

Meta

rustc --version --verbose:

rustc 1.51.0-nightly (04caa632d 2021-01-30)
binary: rustc
commit-hash: 04caa632dd10c2bf64b69524c7f9c4c30a436877
commit-date: 2021-01-30
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0.1

Activity

  1. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    on Feb 1, 2021
  2. emilio commented on Feb 1, 2021

    @emilio
    ContributorAuthor

    Seems like this is a regression from d3c69a4.

  3. emilio commented on Feb 1, 2021

    @emilio
    ContributorAuthor
  4. sanxiyn commented on Feb 2, 2021

    @sanxiyn
    Contributor

    Oops, sorry! Technically, Foo.data field is, in fact, never read. What went wrong is Foo.data is written and read through other ways.

    I think restricting the lint to non-reference fields should work, and will submit a PR to do so quickly. Please double check whether my thinking is correct.

  5. emilio commented on Feb 2, 2021

    @emilio
    ContributorAuthor

    Ah, yeah, I somehow thought that the macro was needed to trigger the bug but it is not. That sounds good. Writes to mutable references have side effects so doing that seems ok.

  6. djc commented on Feb 8, 2021

    @djc
    Contributor

    Would that also cover this case here, in the idna crate? (The Mapper::errors field is a &mut reference to an Errors type, which is in fact returned to the caller of processing().)

    https://github.com/servo/rust-url/pull/674/checks?check_run_id=1855869353

  7. emilio commented on Feb 8, 2021

    @emilio
    ContributorAuthor

    Yeah, I think it's the same.

  8. added a commit that references this issue on Feb 20, 2021
    3b80aef
  9. added a commit that references this issue on Feb 22, 2021
    b62f759
  10. added a commit that references this issue on Feb 23, 2021
    9d378b3
  11. pnkfelix commented on Mar 10, 2021

    @pnkfelix
    Contributor

    Ah, this was closed, but the bug leaked into beta (as mentioned in #81658) while the fix is only in nightly.

    Reopening and tagging as stable-to-beta regression

  12. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 10, 2021
  13. 6 remaining items

  14. pnkfelix commented on Mar 15, 2021

    @pnkfelix
    Contributor

    As noted on #81473 (comment) , we're going to resolve the beta issue by reverting PR #81473 on beta (which I have assigned myself to take care of).

  15. added a commit that references this issue on Mar 18, 2021
  16. modified the milestones: 1.52.0, 1.51.0 on Apr 30, 2021
  17. emilyalbini commented on Apr 30, 2021

    @emilyalbini
    Member

    This was a regression affecting beta 1.51, closing this.

  18. added 5 commits that reference this issue on Aug 11, 2025
  19. added 2 commits that reference this issue on Aug 12, 2025
  20. added a commit that references this issue on Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions