Skip to content

Commit f5a100e

Browse files
bors[bot]Veykril
andauthored
Merge #9514
9514: internal: Invert boolean literals in assist negation logic r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 112e53f + d91704c commit f5a100e

File tree

6 files changed

+50
-51
lines changed

6 files changed

+50
-51
lines changed

crates/ide_assists/src/handlers/auto_import.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ fn group_label(import_candidate: &ImportCandidate) -> GroupLabel {
144144
#[cfg(test)]
145145
mod tests {
146146
use super::*;
147+
147148
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
148149

149150
#[test]

crates/ide_assists/src/handlers/early_return.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ mod tests {
211211
r#"
212212
fn main() {
213213
bar();
214-
if !true {
214+
if false {
215215
return;
216216
}
217217
foo();
@@ -387,7 +387,7 @@ mod tests {
387387
r#"
388388
fn main() {
389389
while true {
390-
if !true {
390+
if false {
391391
continue;
392392
}
393393
foo();
@@ -444,7 +444,7 @@ mod tests {
444444
r#"
445445
fn main() {
446446
loop {
447-
if !true {
447+
if false {
448448
continue;
449449
}
450450
foo();

crates/ide_assists/src/handlers/inline_local_variable.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,35 +65,35 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O
6565
Some(u) => u,
6666
None => return Some(false),
6767
};
68-
Some(
69-
!(matches!(
70-
initializer_expr,
71-
ast::Expr::CallExpr(_)
72-
| ast::Expr::IndexExpr(_)
73-
| ast::Expr::MethodCallExpr(_)
74-
| ast::Expr::FieldExpr(_)
75-
| ast::Expr::TryExpr(_)
76-
| ast::Expr::RefExpr(_)
77-
| ast::Expr::Literal(_)
78-
| ast::Expr::TupleExpr(_)
79-
| ast::Expr::ArrayExpr(_)
80-
| ast::Expr::ParenExpr(_)
81-
| ast::Expr::PathExpr(_)
82-
| ast::Expr::BlockExpr(_)
83-
| ast::Expr::EffectExpr(_),
84-
) || matches!(
85-
usage_parent,
86-
ast::Expr::CallExpr(_)
87-
| ast::Expr::TupleExpr(_)
88-
| ast::Expr::ArrayExpr(_)
89-
| ast::Expr::ParenExpr(_)
90-
| ast::Expr::ForExpr(_)
91-
| ast::Expr::WhileExpr(_)
92-
| ast::Expr::BreakExpr(_)
93-
| ast::Expr::ReturnExpr(_)
94-
| ast::Expr::MatchExpr(_)
95-
)),
96-
)
68+
let initializer = matches!(
69+
initializer_expr,
70+
ast::Expr::CallExpr(_)
71+
| ast::Expr::IndexExpr(_)
72+
| ast::Expr::MethodCallExpr(_)
73+
| ast::Expr::FieldExpr(_)
74+
| ast::Expr::TryExpr(_)
75+
| ast::Expr::RefExpr(_)
76+
| ast::Expr::Literal(_)
77+
| ast::Expr::TupleExpr(_)
78+
| ast::Expr::ArrayExpr(_)
79+
| ast::Expr::ParenExpr(_)
80+
| ast::Expr::PathExpr(_)
81+
| ast::Expr::BlockExpr(_)
82+
| ast::Expr::EffectExpr(_),
83+
);
84+
let parent = matches!(
85+
usage_parent,
86+
ast::Expr::CallExpr(_)
87+
| ast::Expr::TupleExpr(_)
88+
| ast::Expr::ArrayExpr(_)
89+
| ast::Expr::ParenExpr(_)
90+
| ast::Expr::ForExpr(_)
91+
| ast::Expr::WhileExpr(_)
92+
| ast::Expr::BreakExpr(_)
93+
| ast::Expr::ReturnExpr(_)
94+
| ast::Expr::MatchExpr(_)
95+
);
96+
Some(!(initializer || parent))
9797
})
9898
.collect::<Option<_>>()
9999
.map(|b| (file_id, b))

crates/ide_assists/src/handlers/invert_if.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::{
1111

1212
// Assist: invert_if
1313
//
14-
// Apply invert_if
1514
// This transforms if expressions of the form `if !x {A} else {B}` into `if x {B} else {A}`
1615
// This also works with `!=`. This assist can only be applied with the cursor
1716
// on `if`.

crates/ide_assists/src/handlers/replace_let_with_if_let.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) ->
4141
let let_stmt = let_kw.ancestors().find_map(ast::LetStmt::cast)?;
4242
let init = let_stmt.initializer()?;
4343
let original_pat = let_stmt.pat()?;
44-
let ty = ctx.sema.type_of_expr(&init)?;
45-
let happy_variant = TryEnum::from_ty(&ctx.sema, &ty).map(|it| it.happy_case());
4644

4745
let target = let_kw.text_range();
4846
acc.add(
4947
AssistId("replace_let_with_if_let", AssistKind::RefactorRewrite),
5048
"Replace with if-let",
5149
target,
5250
|edit| {
51+
let ty = ctx.sema.type_of_expr(&init);
52+
let happy_variant =
53+
ty.and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty)).map(|it| it.happy_case());
5354
let pat = match happy_variant {
5455
None => original_pat,
5556
Some(var_name) => {

crates/ide_assists/src/utils.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub(crate) mod suggest_name;
44

55
use std::ops;
66

7-
use ast::TypeBoundsOwner;
87
use hir::{Adt, HasSource, Semantics};
98
use ide_db::{
109
helpers::{FamousDefs, SnippetCap},
@@ -14,10 +13,11 @@ use ide_db::{
1413
use itertools::Itertools;
1514
use stdx::format_to;
1615
use syntax::{
17-
ast::edit::AstNodeEdit,
18-
ast::AttrsOwner,
19-
ast::NameOwner,
20-
ast::{self, edit, make, ArgListOwner, GenericParamsOwner},
16+
ast::{
17+
self,
18+
edit::{self, AstNodeEdit},
19+
make, ArgListOwner, AttrsOwner, GenericParamsOwner, NameOwner, TypeBoundsOwner,
20+
},
2121
ted, AstNode, Direction, SmolStr,
2222
SyntaxKind::*,
2323
SyntaxNode, TextSize, T,
@@ -70,11 +70,7 @@ pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> {
7070
pub fn test_related_attribute(fn_def: &ast::Fn) -> Option<ast::Attr> {
7171
fn_def.attrs().find_map(|attr| {
7272
let path = attr.path()?;
73-
if path.syntax().text().to_string().contains("test") {
74-
Some(attr)
75-
} else {
76-
None
77-
}
73+
path.syntax().text().to_string().contains("test").then(|| attr)
7874
})
7975
}
8076

@@ -216,10 +212,7 @@ pub(crate) fn invert_boolean_expression(
216212
sema: &Semantics<RootDatabase>,
217213
expr: ast::Expr,
218214
) -> ast::Expr {
219-
if let Some(expr) = invert_special_case(sema, &expr) {
220-
return expr;
221-
}
222-
make::expr_prefix(T![!], expr)
215+
invert_special_case(sema, &expr).unwrap_or_else(|| make::expr_prefix(T![!], expr))
223216
}
224217

225218
fn invert_special_case(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<ast::Expr> {
@@ -264,8 +257,13 @@ fn invert_special_case(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Opti
264257
pe.expr()
265258
}
266259
}
267-
// FIXME:
268-
// ast::Expr::Literal(true | false )
260+
ast::Expr::Literal(lit) => match lit.kind() {
261+
ast::LiteralKind::Bool(b) => match b {
262+
true => Some(ast::Expr::Literal(make::expr_literal("false"))),
263+
false => Some(ast::Expr::Literal(make::expr_literal("true"))),
264+
},
265+
_ => None,
266+
},
269267
_ => None,
270268
}
271269
}

0 commit comments

Comments
 (0)