Skip to content

Detecting mutation in debug_assert! #1526

Closed
@alex-ozdemir

Description

@alex-ozdemir

Problem

Hey there. I stumbled upon a possible use case for clippy: detecting mutation inside debug_assert! (and its two sister macros).

Why might this be a problem? Consider this program:

fn main() {
    let mut l = vec![0];
    debug_assert!(l.pop().is_some());
    println!("{:?}", l.len());
}

And notice the difference when it is compiled with an without optimization

$ rustc -O debug.rs && ./debug 
1
$ rustc debug.rs && ./debug 
0

Generally this is undesirable.

Solution

I wanted to ask how solvable this is. As I understand it, by the time the compiler has type information (which would be needed to approximately detect mutation), macros have long-since been expanded. Would it be possible to find out whether a potentially-mutating statement

  1. Was in the user's original source?
  2. Was originally inside one of the debug_assert* macros?

Any advice would be much appreciated!

Other Things

Perhaps this is not worth linting -- is there a significant class of false positive?

Also, the embarrassing evidence that this can be a problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsC-an-interesting-projectCategory: Interesting projects, that usually are more involved design/code wise.E-mediumCall for participation: Medium difficulty level problem and requires some initial experience.T-macrosType: Issues with macros and macro expansionT-middleType: Probably requires verifiying types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions