Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mut_mut suggests invalid code #9035

Closed
jo-so opened this issue Jun 22, 2022 · 1 comment · Fixed by #9835
Closed

mut_mut suggests invalid code #9035

jo-so opened this issue Jun 22, 2022 · 1 comment · Fixed by #9835
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@jo-so
Copy link

jo-so commented Jun 22, 2022

Summary

Clippy suggests to re-borrow a struct field which leads to invalid code. The field is of type &mut dyn …

Lint Name

mut_mut

Reproducer

I tried this code:

#![allow(dead_code)]
#![warn(clippy::pedantic)]

use std::fmt::Display;

struct Foo<'a> {
    inner: &'a mut dyn Display,
}

impl Foo<'_> {
    fn foo(&mut self) {
        let hlp = &mut self.inner;
        bar(hlp);
    }
}

fn bar(_: &mut impl Display) { }

I saw this happen:

warning: this expression mutably borrows a mutable reference. Consider reborrowing
  --> src/main.rs:12:19
   |
12 |         let hlp = &mut self.inner;
   |                   ^^^^^^^^^^^^^^^

I expected to see this happen:

The reborrowing let hlp = &mut *self.inner; doesn't compile:

error[E0277]: the size for values of type `dyn std::fmt::Display` cannot be known at compilation time
  --> src/main.rs:13:13
   |
13 |         bar(hlp);
   |         --- ^^^ doesn't have a size known at compile-time
   |         |
   |         required by a bound introduced by this call
   |
   = help: the trait `Sized` is not implemented for `dyn std::fmt::Display`

Version

rustc 1.63.0-nightly (21e9336fe 2022-06-18)
binary: rustc
commit-hash: 21e9336fe81a1fce364349bb7a35a0347c369f34
commit-date: 2022-06-18
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.5

Additional Labels

No response

@jo-so jo-so added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jun 22, 2022
@koka831
Copy link
Contributor

koka831 commented Nov 12, 2022

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants