Skip to content

Commit

Permalink
Format let-else with rustfmt nightly (#5461)
Browse files Browse the repository at this point in the history
Support for `let…else` formatting was just merged to nightly
(rust-lang/rust#113225). Rerun `cargo fmt` with Rust nightly 2023-07-02
to pick this up. Followup to #939.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Loading branch information
andersk authored Jul 3, 2023
1 parent c8b9a46 commit df13e69
Show file tree
Hide file tree
Showing 105 changed files with 782 additions and 362 deletions.
2 changes: 1 addition & 1 deletion crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4435,7 +4435,7 @@ impl<'a> Checker<'a> {
}

fn handle_node_delete(&mut self, expr: &'a Expr) {
let Expr::Name(ast::ExprName { id, .. } )= expr else {
let Expr::Name(ast::ExprName { id, .. }) = expr else {
return;
};

Expand Down
11 changes: 6 additions & 5 deletions crates/ruff/src/jupyter/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,12 @@ impl Notebook {
.markers()
.iter()
.rev()
.find(|m| m.source <= *offset) else {
// There are no markers above the current offset, so we can
// stop here.
break;
};
.find(|m| m.source <= *offset)
else {
// There are no markers above the current offset, so we can
// stop here.
break;
};
last_marker = Some(marker);
marker
}
Expand Down
6 changes: 1 addition & 5 deletions crates/ruff/src/rules/flake8_annotations/rules/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,7 @@ pub(crate) fn definition(
// TODO(charlie): Consider using the AST directly here rather than `Definition`.
// We could adhere more closely to `flake8-annotations` by defining public
// vs. secret vs. protected.
let Definition::Member(Member {
kind,
stmt,
..
}) = definition else {
let Definition::Member(Member { kind, stmt, .. }) = definition else {
return vec![];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn unparse_string_format_expression(checker: &mut Checker, expr: &Expr) -> Optio
return None;
};
// Only evaluate the full BinOp, not the nested components.
let Expr::BinOp(_ )= parent else {
let Expr::BinOp(_) = parent else {
if any_over_expr(expr, &has_string_literal) {
return Some(checker.generator().expr(expr));
}
Expand Down
26 changes: 13 additions & 13 deletions crates/ruff/src/rules/flake8_bugbear/rules/abstract_base_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,19 @@ pub(crate) fn abstract_base_class(
continue;
}

let (
Stmt::FunctionDef(ast::StmtFunctionDef {
decorator_list,
body,
name: method_name,
..
}) | Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
decorator_list,
body,
name: method_name,
..
})
) = stmt else {
let (Stmt::FunctionDef(ast::StmtFunctionDef {
decorator_list,
body,
name: method_name,
..
})
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
decorator_list,
body,
name: method_name,
..
})) = stmt
else {
continue;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ impl fmt::Display for ExceptionKind {
/// B017
pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem]) {
for item in items {
let Expr::Call(ast::ExprCall { func, args, keywords, range: _ }) = &item.context_expr else {
let Expr::Call(ast::ExprCall {
func,
args,
keywords,
range: _,
}) = &item.context_expr
else {
return;
};
if args.len() != 1 {
Expand All @@ -91,13 +97,14 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem])
let Some(exception) = checker
.semantic()
.resolve_call_path(args.first().unwrap())
.and_then(|call_path| {
match call_path.as_slice() {
["", "Exception"] => Some(ExceptionKind::Exception),
["", "BaseException"] => Some(ExceptionKind::BaseException),
_ => None,
}
}) else { return; };
.and_then(|call_path| match call_path.as_slice() {
["", "Exception"] => Some(ExceptionKind::Exception),
["", "BaseException"] => Some(ExceptionKind::BaseException),
_ => None,
})
else {
return;
};

let assertion = if matches!(func.as_ref(), Expr::Attribute(ast::ExprAttribute { attr, .. }) if attr == "assertRaises")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) fn assignment_to_os_environ(checker: &mut Checker, targets: &[Expr])
if attr != "environ" {
return;
}
let Expr::Name(ast::ExprName { id, .. } )= value.as_ref() else {
let Expr::Name(ast::ExprName { id, .. }) = value.as_ref() else {
return;
};
if id != "os" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ pub(crate) fn duplicate_exceptions(checker: &mut Checker, handlers: &[ExceptHand
let mut seen: FxHashSet<CallPath> = FxHashSet::default();
let mut duplicates: FxHashMap<CallPath, Vec<&Expr>> = FxHashMap::default();
for handler in handlers {
let ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { type_: Some(type_), .. }) = handler else {
let ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
type_: Some(type_),
..
}) = handler
else {
continue;
};
match type_.as_ref() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Violation for ExceptWithNonExceptionClasses {
/// This should leave any unstarred iterables alone (subsequently raising a
/// warning for B029).
fn flatten_starred_iterables(expr: &Expr) -> Vec<&Expr> {
let Expr::Tuple(ast::ExprTuple { elts, .. } )= expr else {
let Expr::Tuple(ast::ExprTuple { elts, .. }) = expr else {
return vec![expr];
};
let mut flattened_exprs: Vec<&Expr> = Vec::with_capacity(elts.len());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub(crate) fn getattr_with_constant(
func: &Expr,
args: &[Expr],
) {
let Expr::Name(ast::ExprName { id, .. } )= func else {
let Expr::Name(ast::ExprName { id, .. }) = func else {
return;
};
if id != "getattr" {
Expand All @@ -76,7 +76,8 @@ pub(crate) fn getattr_with_constant(
let Expr::Constant(ast::ExprConstant {
value: Constant::Str(value),
..
} )= arg else {
}) = arg
else {
return;
};
if !is_identifier(value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub(crate) fn mutable_argument_default(checker: &mut Checker, arguments: &Argume
.chain(&arguments.args)
.chain(&arguments.kwonlyargs)
{
let Some(default)= default else {
let Some(default) = default else {
continue;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ pub(crate) fn redundant_tuple_in_exception_handler(
handlers: &[ExceptHandler],
) {
for handler in handlers {
let ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { type_: Some(type_), .. }) = handler else {
let ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
type_: Some(type_),
..
}) = handler
else {
continue;
};
let Expr::Tuple(ast::ExprTuple { elts, .. }) = type_.as_ref() else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ pub(crate) fn setattr_with_constant(
let Expr::Constant(ast::ExprConstant {
value: Constant::Str(name),
..
} )= name else {
}) = name
else {
return;
};
if !is_identifier(name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub(crate) fn star_arg_unpacking_after_keyword_arg(
return;
};
for arg in args {
let Expr::Starred (_) = arg else {
let Expr::Starred(_) = arg else {
continue;
};
if arg.start() <= keyword.start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ pub(crate) fn strip_with_multi_characters(
let Expr::Constant(ast::ExprConstant {
value: Constant::Str(value),
..
} )= &args[0] else {
}) = &args[0]
else {
return;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub(crate) fn unary_prefix_increment(
if !matches!(op, UnaryOp::UAdd) {
return;
}
let Expr::UnaryOp(ast::ExprUnaryOp { op, .. })= operand else {
return;
};
let Expr::UnaryOp(ast::ExprUnaryOp { op, .. }) = operand else {
return;
};
if !matches!(op, UnaryOp::UAdd) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pub(crate) fn unreliable_callable_check(
let Expr::Constant(ast::ExprConstant {
value: Constant::Str(s),
..
}) = &args[1] else
{
}) = &args[1]
else {
return;
};
if s != "__call__" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ pub(crate) fn zip_without_explicit_strict(
/// Return `true` if the [`Expr`] appears to be an infinite iterator (e.g., a call to
/// `itertools.cycle` or similar).
fn is_infinite_iterator(arg: &Expr, semantic: &SemanticModel) -> bool {
let Expr::Call(ast::ExprCall { func, args, keywords, .. }) = &arg else {
let Expr::Call(ast::ExprCall {
func,
args,
keywords,
..
}) = &arg
else {
return false;
};

Expand Down
22 changes: 9 additions & 13 deletions crates/ruff/src/rules/flake8_comprehensions/fixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ pub(crate) fn fix_unnecessary_generator_dict(
// Extract the (k, v) from `(k, v) for ...`.
let generator_exp = match_generator_exp(&arg.value)?;
let tuple = match_tuple(&generator_exp.elt)?;
let [Element::Simple { value: key, .. }, Element::Simple { value, .. }] = &tuple.elements[..] else {
let [Element::Simple { value: key, .. }, Element::Simple { value, .. }] = &tuple.elements[..]
else {
bail!("Expected tuple to contain two elements");
};

Expand Down Expand Up @@ -188,9 +189,10 @@ pub(crate) fn fix_unnecessary_list_comprehension_dict(

let tuple = match_tuple(&list_comp.elt)?;

let [Element::Simple {
value: key, ..
}, Element::Simple { value, .. }] = &tuple.elements[..] else { bail!("Expected tuple with two elements"); };
let [Element::Simple { value: key, .. }, Element::Simple { value, .. }] = &tuple.elements[..]
else {
bail!("Expected tuple with two elements");
};

tree = Expression::DictComp(Box::new(DictComp {
key: Box::new(key.clone()),
Expand Down Expand Up @@ -982,14 +984,10 @@ pub(crate) fn fix_unnecessary_map(
}

let Some(Element::Simple { value: key, .. }) = &tuple.elements.get(0) else {
bail!(
"Expected tuple to contain a key as the first element"
);
bail!("Expected tuple to contain a key as the first element");
};
let Some(Element::Simple { value, .. }) = &tuple.elements.get(1) else {
bail!(
"Expected tuple to contain a key as the second element"
);
bail!("Expected tuple to contain a key as the second element");
};

(key, value)
Expand Down Expand Up @@ -1063,9 +1061,7 @@ pub(crate) fn fix_unnecessary_comprehension_any_all(
let call = match_call_mut(&mut tree)?;

let Expression::ListComp(list_comp) = &call.args[0].value else {
bail!(
"Expected Expression::ListComp"
);
bail!("Expected Expression::ListComp");
};

let mut new_empty_lines = vec![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ pub(crate) fn unnecessary_comprehension_any_all(
if !keywords.is_empty() {
return;
}
let Expr::Name(ast::ExprName { id, .. } )= func else {
let Expr::Name(ast::ExprName { id, .. }) = func else {
return;
};
if (matches!(id.as_str(), "all" | "any")) && args.len() == 1 {
let (Expr::ListComp(ast::ExprListComp { elt, .. } )| Expr::SetComp(ast::ExprSetComp { elt, .. })) = &args[0] else {
let (Expr::ListComp(ast::ExprListComp { elt, .. })
| Expr::SetComp(ast::ExprSetComp { elt, .. })) = &args[0]
else {
return;
};
if contains_await(elt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub(crate) fn unnecessary_double_cast_or_process(
let Some(arg) = args.first() else {
return;
};
let Expr::Call(ast::ExprCall { func, ..} )= arg else {
let Expr::Call(ast::ExprCall { func, .. }) = arg else {
return;
};
let Some(inner) = helpers::expr_name(func) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ pub(crate) fn unnecessary_generator_dict(
args: &[Expr],
keywords: &[Keyword],
) {
let Some(argument) = helpers::exactly_one_argument_with_matching_function("dict", func, args, keywords) else {
let Some(argument) =
helpers::exactly_one_argument_with_matching_function("dict", func, args, keywords)
else {
return;
};
if let Expr::GeneratorExp(ast::ExprGeneratorExp { elt, .. }) = argument {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ pub(crate) fn unnecessary_generator_list(
args: &[Expr],
keywords: &[Keyword],
) {
let Some(argument) = helpers::exactly_one_argument_with_matching_function("list", func, args, keywords) else {
let Some(argument) =
helpers::exactly_one_argument_with_matching_function("list", func, args, keywords)
else {
return;
};
if !checker.semantic().is_builtin("list") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ pub(crate) fn unnecessary_generator_set(
args: &[Expr],
keywords: &[Keyword],
) {
let Some(argument) = helpers::exactly_one_argument_with_matching_function("set", func, args, keywords) else {
let Some(argument) =
helpers::exactly_one_argument_with_matching_function("set", func, args, keywords)
else {
return;
};
if !checker.semantic().is_builtin("set") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ pub(crate) fn unnecessary_list_comprehension_dict(
args: &[Expr],
keywords: &[Keyword],
) {
let Some(argument) = helpers::exactly_one_argument_with_matching_function("dict", func, args, keywords) else {
let Some(argument) =
helpers::exactly_one_argument_with_matching_function("dict", func, args, keywords)
else {
return;
};
if !checker.semantic().is_builtin("dict") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ pub(crate) fn unnecessary_list_comprehension_set(
args: &[Expr],
keywords: &[Keyword],
) {
let Some(argument) = helpers::exactly_one_argument_with_matching_function("set", func, args, keywords) else {
let Some(argument) =
helpers::exactly_one_argument_with_matching_function("set", func, args, keywords)
else {
return;
};
if !checker.semantic().is_builtin("set") {
Expand Down
Loading

0 comments on commit df13e69

Please sign in to comment.