@@ -124,47 +124,37 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
124124 op_range,
125125 |builder| {
126126 let make = SyntaxFactory :: with_mappings ( ) ;
127- let paren_expr = bin_expr. syntax ( ) . parent ( ) . and_then ( ast:: ParenExpr :: cast) ;
128- let neg_expr = paren_expr
129- . clone ( )
127+ let ( target_node, result_expr) = if let Some ( neg_expr) = bin_expr
128+ . syntax ( )
129+ . parent ( )
130+ . and_then ( ast:: ParenExpr :: cast)
130131 . and_then ( |paren_expr| paren_expr. syntax ( ) . parent ( ) )
131132 . and_then ( ast:: PrefixExpr :: cast)
132133 . filter ( |prefix_expr| matches ! ( prefix_expr. op_kind( ) , Some ( ast:: UnaryOp :: Not ) ) )
133- . map ( ast:: Expr :: PrefixExpr ) ;
134-
135- let mut editor;
136- if let Some ( paren_expr) = paren_expr {
137- if let Some ( neg_expr) = neg_expr {
138- cov_mark:: hit!( demorgan_double_negation) ;
139- let parent = neg_expr. syntax ( ) . parent ( ) ;
140- editor = builder. make_editor ( neg_expr. syntax ( ) ) ;
141-
142- if parent. is_some_and ( |parent| {
143- demorganed. needs_parens_in_place_of ( & parent, neg_expr. syntax ( ) )
144- } ) {
145- cov_mark:: hit!( demorgan_keep_parens_for_op_precedence2) ;
146- editor. replace ( neg_expr. syntax ( ) , make. expr_paren ( demorganed) . syntax ( ) ) ;
147- } else {
148- editor. replace ( neg_expr. syntax ( ) , demorganed. syntax ( ) ) ;
149- } ;
150- } else {
151- cov_mark:: hit!( demorgan_double_parens) ;
152- editor = builder. make_editor ( paren_expr. syntax ( ) ) ;
153-
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- }
162- }
134+ {
135+ cov_mark:: hit!( demorgan_double_negation) ;
136+ ( ast:: Expr :: from ( neg_expr) . syntax ( ) . clone ( ) , demorganed)
137+ } else if let Some ( paren_expr) =
138+ bin_expr. syntax ( ) . parent ( ) . and_then ( ast:: ParenExpr :: cast)
139+ {
140+ cov_mark:: hit!( demorgan_double_parens) ;
141+ ( paren_expr. syntax ( ) . clone ( ) , add_bang_paren ( & make, demorganed) )
163142 } else {
164- editor = builder. make_editor ( bin_expr. syntax ( ) ) ;
165- editor. replace ( bin_expr. syntax ( ) , add_bang_paren ( & make, demorganed) . syntax ( ) ) ;
166- }
143+ ( bin_expr. syntax ( ) . clone ( ) , add_bang_paren ( & make, demorganed) )
144+ } ;
145+
146+ let final_expr = if target_node
147+ . parent ( )
148+ . is_some_and ( |p| result_expr. needs_parens_in_place_of ( & p, & target_node) )
149+ {
150+ cov_mark:: hit!( demorgan_keep_parens_for_op_precedence2) ;
151+ make. expr_paren ( result_expr) . into ( )
152+ } else {
153+ result_expr
154+ } ;
167155
156+ let mut editor = builder. make_editor ( & target_node) ;
157+ editor. replace ( & target_node, final_expr. syntax ( ) ) ;
168158 editor. add_mappings ( make. finish_with_mappings ( ) ) ;
169159 builder. add_file_edits ( ctx. vfs_file_id ( ) , editor) ;
170160 } ,
0 commit comments