rust-lang / rust-lang/rust-clippy

`collapsible_if` false positive with RefCell borrow

Open
#7,513 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Lint name: collapsible_if

I tried this code:

use std::cell::RefCell;

struct Test {
    a: usize,
    b: RefCell<usize>,
}

fn main() {
    let mut t = Test {
        a: 1,
        b: RefCell::new(2),
    };
                                   
    if *t.b.borrow() == 0 {    
        if (&mut t).a == 5 {       
            println!("a");         
        }                          
    }                              
}                                  

Clippy suggests this change which does not compile because it requires borrowing t as mutable while it is already borrowed as immutable.

 if *t.b.borrow() == 0 && (&mut t).a == 5 {
     println!("a");
 }
Meta
  • cargo clippy -V: clippy 0.1.54 (a178d03 2021-07-26)
  • rustc -Vv:
rustc 1.54.0 (a178d0322 2021-07-26)
binary: rustc
commit-hash: a178d0322ce20e33eac124758e837cbd80a6f633
commit-date: 2021-07-26
host: x86_64-unknown-linux-gnu
release: 1.54.0
LLVM version: 12.0.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the collapsible_if warning with the Rust code in the issue and cargo clippy. Trace the collapsible_if lint and its existing tests, then add coverage for the RefCell borrow case. Done means Clippy no longer suggests a transformation that fails to compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.