rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.19k stars 1.59k forks source link

Invoking the dbg! macro snippet on a negated expression leaves the negation operator outside the dbg!() #11439

Open petr-tik opened 2 years ago

petr-tik commented 2 years ago

Expected

Calling the postfix dbg completion on a negated expression will wrap the whole expression including the negation operator.

Observed

When the postfix dbg completion is requested, the original negation operator remains outside the freshly inserted dbg!() macro.

Repro

enum NestedStuff {
    Variants,
    Dont,
    Matter,
}

impl NestedStuff {
    fn random_method(self) -> bool {
        return true;
    }
}

fn some_func() {
    let my_enum = NestedStuff::Matter;
    if !my_enum.random_method().dbg$0 {  // choose the dbg snippet and have this line convert to 
 //`if !dbg!(my_enum.random_method()) {`
        println!("Hello");
    }
}

Version info

rustc -V
rustc 1.58.0 (02072b482 2022-01-11)

rust-analyzer --version
rust-analyzer ba3305480 2022-02-07 stable

using lsp-mode in doom emacs, if that's relevant.

jhgg commented 2 years ago

This seems like intended behavior. At the very least, I like that it functions the way it does currently!

petr-tik commented 2 years ago

I often use dbg!() for debug printing and found it confusing when dbg!() printed a method that returns true, but the branch wasn't actually taken, because the negation operator outside dbg!() converted the value to false. I would find it more obvious, if the dbg postfix completion included negation by default to prevent such surprises.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a51870009964dee5fa1c615d4b65014a

CGMossa commented 2 years ago

This also happens with .if completion.

Veykril commented 2 years ago

Ye if is a definite contender here as well, we should look at all the postfixes and check where checking the precedence might make sense