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

Can't ignore clippy::borrow_deref_ref #8971

Closed
tokatoka opened this issue Jun 8, 2022 · 10 comments · Fixed by #10761
Closed

Can't ignore clippy::borrow_deref_ref #8971

tokatoka opened this issue Jun 8, 2022 · 10 comments · Fixed by #10761
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@tokatoka
Copy link

tokatoka commented Jun 8, 2022

Summary

We have this function in our crate https://github.com/AFLplusplus/LibAFL/tree/main/libafl
https://github.com/AFLplusplus/LibAFL/blob/main/libafl/src/observers/mod.rs#L816

        #[pyo3(name = "match_name")]
        #[allow(clippy::all)]
        fn pymatch_name(&self, name: &str) -> Option<PythonObserver> {
            for ob in &self.list {
                if *ob.name() == *name {
                    return Some(ob.clone());
                }
            }
            None
        }

and clippy gives this warning

error: deref on an immutable reference
   --> libafl/src/observers/mod.rs:817:38
    |
817 |         fn pymatch_name(&self, name: &str) -> Option<PythonObserver> {
    |                                      ^^^^ help: if you would like to reborrow, try removing `&*`: `&str`
    |
note: the lint level is defined here
   --> libafl/src/lib.rs:14:9
    |
14  | #![deny(clippy::all)]
    |         ^^^^^^^^^^^
    = note: `#[deny(clippy::borrow_deref_ref)]` implied by `#[deny(clippy::all)]`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_deref_ref

We actually have allow(clippy::all) on this function, but still the clippy gives this error and it's not ignored.

Lint Name

borrow_deref_ref

Reproducer

You can clone the repo https://github.com/AFLplusplus/LibAFL/tree/main/libafl
and checkout to debug_restarting branch
and run

RUST_BACKTRACE=full cargo +nightly clippy --all --all-features --release --tests -- -Z macro-backtrace    -D clippy::all    -D clippy::pedantic    -W clippy::similar_names    -A clippy::type_repetition_in_bounds    -A clippy::missing-errors-doc    -A clippy::cast-possible-truncation    -A clippy::used-underscore-binding    -A clippy::ptr-as-ptr    -A clippy::missing-panics-doc    -A clippy::missing-docs-in-private-items    -A clippy::unseparated-literal-suffix    -A clippy::module-name-repetitions    -A clippy::unreadable-literal

or simply

./script/clippy.sh

Version

rustc 1.63.0-nightly (5435ed691 2022-06-07)
binary: rustc
commit-hash: 5435ed6916a59e8d5acba2149316a841c3905cbd
commit-date: 2022-06-07
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.5

Additional Labels

No response

@tokatoka tokatoka 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 8, 2022
@andreafioraldi
Copy link

Related to #7930 I guess

@Jarcho
Copy link
Contributor

Jarcho commented Jun 9, 2022

Fixing this is waiting on #8694.

In the meantime you can allow at the module or crate level.

@xFrednet
Copy link
Member

xFrednet commented Jun 9, 2022

rust-lang/rust#97660 is also related to this, as the lint is currently not emitted at the correct location.

@tgross35
Copy link
Contributor

Is there a reason this lint tends to be emitted with pyO3 projects? I am running in to the same thing on a python crate and scratching my head a bit

Even simpler for me, I don't even use the * operator

#[pyfunction]
#[inline]
fn levenshtein(a: &str, b: &str, limit: Option<u32>) -> u32 {
    match limit {
        Some(v) => algorithms::levenshtein_limit(a, b, v),
        None => algorithms::levenshtein(a, b),
    }
}

@xFrednet
Copy link
Member

That is most likely a problem originating from the #[pyfunction] macro, we have a pr to improve detection of these. Hopefully, this case should be covered by that PR #8694 🙃

@Rikorose
Copy link

I can still confirm on latest rust 1.63.0 stable clippy that fucntions implmented within a #[pymethods] impl still result in this lint:

#[pymethods]
impl _FdDataLoader {
  // ...
    fn start_epoch(&mut self, split: &str, seed: usize) -> PyResult<()> {
      // ...
    }
}
warning: deref on an immutable reference
   --> pyDF-data/src/lib.rs:221:38
    |
221 |     fn start_epoch(&mut self, split: &str, seed: usize) -> PyResult<()> {
    |                                      ^^^^ help: if you would like to reborrow, try removing `&*`: `&str`
    |
    = note: `#[warn(clippy::borrow_deref_ref)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_deref_ref

@xFrednet
Copy link
Member

xFrednet commented Aug 13, 2022

The PR #8694 took a longer time to review. It should hit stable in two releases, so by 1.65. The lint mentioned didn't get an extra check, though. Adding it should be simple with the new util functions from of the PR. (See is_from_proc_macro) 🙃

@rustbot label +good-first-issue

@lochetti
Copy link
Contributor

lochetti commented May 3, 2023

Hi! I am trying to simulate the clippy warning here because I want to try to solve this issue. But I am having hard time to reproduce the lint warn.

Can you please help me out?

The code sample that I am using to try to reproduce is:

use pyo3::prelude::*;

fn main() {
    concat_strings("foo", "bar").ok();
}

#[pyfunction]
#[pyo3(name = "concat_strings")]
fn concat_strings(a: &str, b: &str) -> PyResult<String> {
    Ok(format!("{a}{b}"))
}

Just want to be sure if this snippet should trigger the lint warn or not. If not, can you try to provide another small snippet that triggers the warning?

PS: I am using pyo3 v0.18.3

@xFrednet
Copy link
Member

xFrednet commented May 3, 2023

I would guess that the triggering code has been fixed in pyfunction. However, the problem of proc macros still exists. As suggested in my previous comment, a check with is_from_proc_macro should be added to the lint. We have some lints that use that already and some tests macros which can be used :)

@lochetti
Copy link
Contributor

lochetti commented May 3, 2023

Hey @xFrednet thanks for pointing out! I have added similar check to another lint, so I guess that I will be able to add here as well. I just wanted to try to reproduce the problem with pyo3 before, to make sure that the is_from_proc_macro check would fix the "original issue" :)

Anyway, I will continue here with the is_from_proc_macro approach.
Thanks again :)

@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 good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
8 participants