@@ -151,7 +151,14 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
151151 cov_mark:: hit!( demorgan_double_parens) ;
152152 editor = builder. make_editor ( paren_expr. syntax ( ) ) ;
153153
154- editor. replace ( paren_expr. syntax ( ) , add_bang_paren ( & make, demorganed) . syntax ( ) ) ;
154+ let new_expr = add_bang_paren ( & make, demorganed) ;
155+ if paren_expr. syntax ( ) . parent ( ) . is_some_and ( |parent| {
156+ new_expr. needs_parens_in_place_of ( & parent, paren_expr. syntax ( ) )
157+ } ) {
158+ editor. replace ( paren_expr. syntax ( ) , make. expr_paren ( new_expr) . syntax ( ) ) ;
159+ } else {
160+ editor. replace ( paren_expr. syntax ( ) , new_expr. syntax ( ) ) ;
161+ }
155162 }
156163 } else {
157164 editor = builder. make_editor ( bin_expr. syntax ( ) ) ;
@@ -636,4 +643,31 @@ fn main() {
636643"# ,
637644 ) ;
638645 }
646+
647+ #[ test]
648+ fn demorgan_method_call_receiver ( ) {
649+ check_assist (
650+ apply_demorgan,
651+ "fn f() { (x ||$0 !y).then_some(42) }" ,
652+ "fn f() { (!(!x && y)).then_some(42) }" ,
653+ ) ;
654+ }
655+
656+ #[ test]
657+ fn demorgan_method_call_receiver_complex ( ) {
658+ check_assist (
659+ apply_demorgan,
660+ "fn f() { (a && b ||$0 c && d).then_some(42) }" ,
661+ "fn f() { (!(!(a && b) && !(c && d))).then_some(42) }" ,
662+ ) ;
663+ }
664+
665+ #[ test]
666+ fn demorgan_method_call_receiver_chained ( ) {
667+ check_assist (
668+ apply_demorgan,
669+ "fn f() { (a ||$0 b).then_some(42).or(Some(0)) }" ,
670+ "fn f() { (!(!a && !b)).then_some(42).or(Some(0)) }" ,
671+ ) ;
672+ }
639673}
0 commit comments