Skip to content

needless_pass_by_ref_mut should not trigger for cfg-gated mod/fn #11185

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

Closed
oxalica opened this issue Jul 19, 2023 · 6 comments · Fixed by #11226
Closed

needless_pass_by_ref_mut should not trigger for cfg-gated mod/fn #11185

oxalica opened this issue Jul 19, 2023 · 6 comments · Fixed by #11226
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@oxalica
Copy link
Contributor

oxalica commented Jul 19, 2023

Summary

In target specific modules, some functions may only mutate arguments for a specific platform. Keeping the same signature to take &mut for all of them makes higher level abstractions simpler.

If we expose this platform differences, it will propagate upwards and force every users to #[cfg] between & and &mut. This violates the abstraction goal of the library.

A solution to this issue is to treat every #[cfg] fn and #[cfg] mod { pub fn } as part of public interface, and suppress warnings that would cause signature changes on them.

Lint Name

needless_pass_by_ref_mut

Reproducer

pub struct Resource(i32);

impl Resource {
    // Real public interface.
    pub fn work(&mut self) {
        imp::work(&mut self.0);
    }
}

// Semi-public interface. Abstractions over platforms.
#[cfg(unix)]
mod imp {
    // Immutable for some platforms.
    pub fn work(data: &mut i32) { // <- needless_pass_by_ref_mut
        println!("{data}");
    }
}

#[cfg(windows)]
mod imp {
    // Mutable for some other platforms.
    pub fn work(data: &mut i32) {
        *data += 1;
        println!("{data}");
    }
}

I saw this happen:

warning: this argument is a mutable reference, but not used mutably
  --> src/lib.rs:13:23
   |
14 |     pub fn work(data: &mut i32) { // <- needless_pass_by_ref_mut
   |                       ^^^^^^^^ help: consider changing to: `&i32`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
   = note: `#[warn(clippy::needless_pass_by_ref_mut)]` on by default

I expected to see this happen:

Version

Rust Playground, nightly.
Clippy 0.1.73 (2023-07-18 903e279)

Additional Labels

@rustbot label +I-false-positive +I-suggestion-causes-error

I think this fits I-suggestion-causes-error because the change breaks on other cfg-gated platforms.

@oxalica oxalica 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 Jul 19, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jul 19, 2023
@Centri3
Copy link
Member

Centri3 commented Jul 19, 2023

cc @GuillaumeGomez

Likely you'll want to traverse up the DefId tree using parent_module/parent_module_from_def_id to see if any have a #[cfg(...)] attribute, plus the function in question ofc.

@GuillaumeGomez
Copy link
Member

I don't think it's a good idea to disable the lint if we enter a cfg gated item. In this case, it enters the "I know what I'm doing, let's disable the lint for this item" zone.

@Centri3
Copy link
Member

Centri3 commented Jul 19, 2023

Then we could perhaps at least add a note then to allow it if it's inside a cfg, because this is likely rather common.

@GuillaumeGomez
Copy link
Member

I don't understand what you mean by "add a note".

@Centri3
Copy link
Member

Centri3 commented Jul 19, 2023

Should've clarified; like with how show_semver_warning works already, but calling diag.note instead of diag.warn to say something like "this is cfg-gated and may require further changes"

@GuillaumeGomez
Copy link
Member

Oh I see. Yes that sounds like a nice idea indeed.

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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants