]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #12130 - weirane:let-else-let-match, r=weirane
authorbors <bors@rust-lang.org>
Tue, 17 May 2022 19:01:18 +0000 (19:01 +0000)
committerbors <bors@rust-lang.org>
Tue, 17 May 2022 19:01:18 +0000 (19:01 +0000)
Turn let-else statements into let and match

Fixes #11906.

1  2 
crates/ide-assists/src/lib.rs
crates/ide-assists/src/tests/generated.rs

index c1751e8b406ebcdc1cca9ed65d9afa3df31b1030,4896c327198c0c494e790667bafa85372f363a6a..a5dfb5d2953e0da30f6ac3f7551349bc117cedd9
@@@ -37,7 -37,7 +37,7 @@@
  //!   should be available more or less everywhere, which isn't useful. So
  //!   instead we only show it if the user *selects* the items they want to sort.
  //! * Consider grouping related assists together (see [`Assists::add_group`]).
 -//! * Make assists robust. If the assist depends on results of type-inference to
 +//! * Make assists robust. If the assist depends on results of type-inference too
  //!   much, it might only fire in fully-correct code. This makes assist less
  //!   useful and (worse) less predictable. The user should have a clear
  //!   intuition when each particular assist is available.
@@@ -54,6 -54,7 +54,6 @@@
  //!   something. If something *could* be a diagnostic, it should be a
  //!   diagnostic. Conversely, it might be valuable to turn a diagnostic with a
  //!   lot of false errors into an assist.
 -//! *
  //!
  //! See also this post:
  //! <https://rust-analyzer.github.io/blog/2020/09/28/how-to-make-a-light-bulb.html>
@@@ -106,8 -107,6 +106,8 @@@ mod handlers 
      mod add_explicit_type;
      mod add_lifetime_to_type;
      mod add_missing_impl_members;
 +    mod add_missing_match_arms;
 +    mod add_attribute;
      mod add_turbo_fish;
      mod apply_demorgan;
      mod auto_import;
      mod convert_integer_literal;
      mod convert_into_to_from;
      mod convert_iter_for_each_to_for;
+     mod convert_let_else_to_match;
      mod convert_tuple_struct_to_named_struct;
      mod convert_to_guarded_return;
      mod convert_while_to_loop;
      mod extract_struct_from_enum_variant;
      mod extract_type_alias;
      mod extract_variable;
 -    mod add_missing_match_arms;
      mod fix_visibility;
      mod flip_binexpr;
      mod flip_comma;
      mod generate_default_from_enum_variant;
      mod generate_default_from_new;
      mod generate_deref;
 -    mod generate_derive;
      mod generate_documentation_template;
      mod generate_enum_is_method;
      mod generate_enum_projection_method;
      mod remove_mut;
      mod remove_unused_param;
      mod reorder_fields;
 -    mod reorder_impl;
 +    mod reorder_impl_items;
      mod replace_try_expr_with_match;
      mod replace_derive_with_manual_impl;
      mod replace_if_let_with_match;
      pub(crate) fn all() -> &'static [Handler] {
          &[
              // These are alphabetic for the foolish consistency
 +            add_attribute::add_attribute,
              add_explicit_type::add_explicit_type,
              add_missing_match_arms::add_missing_match_arms,
              add_lifetime_to_type::add_lifetime_to_type,
              convert_into_to_from::convert_into_to_from,
              convert_iter_for_each_to_for::convert_iter_for_each_to_for,
              convert_iter_for_each_to_for::convert_for_loop_with_for_each,
+             convert_let_else_to_match::convert_let_else_to_match,
              convert_to_guarded_return::convert_to_guarded_return,
              convert_tuple_struct_to_named_struct::convert_tuple_struct_to_named_struct,
              convert_while_to_loop::convert_while_to_loop,
              generate_constant::generate_constant,
              generate_default_from_enum_variant::generate_default_from_enum_variant,
              generate_default_from_new::generate_default_from_new,
 -            generate_derive::generate_derive,
              generate_documentation_template::generate_documentation_template,
              generate_enum_is_method::generate_enum_is_method,
              generate_enum_projection_method::generate_enum_as_method,
              remove_mut::remove_mut,
              remove_unused_param::remove_unused_param,
              reorder_fields::reorder_fields,
 -            reorder_impl::reorder_impl,
 +            reorder_impl_items::reorder_impl_items,
              replace_try_expr_with_match::replace_try_expr_with_match,
              replace_derive_with_manual_impl::replace_derive_with_manual_impl,
              replace_if_let_with_match::replace_if_let_with_match,
index d3917f6c97ca4c69eb9facc8b11d3b179ece24ef,008dd7eec17474d502fb4c16fe710ebd81b26fdd..7772563b8388840634b950b25fa33b69a26cd54f
@@@ -2,26 -2,6 +2,26 @@@
  
  use super::check_doc_test;
  
 +#[test]
 +fn doctest_add_attribute() {
 +    check_doc_test(
 +        "add_derive",
 +        r#####"
 +struct Point {
 +    x: u32,
 +    y: u32,$0
 +}
 +"#####,
 +        r#####"
 +#[derive($0)]
 +struct Point {
 +    x: u32,
 +    y: u32,
 +}
 +"#####,
 +    )
 +}
 +
  #[test]
  fn doctest_add_explicit_type() {
      check_doc_test(
@@@ -384,6 -364,26 +384,26 @@@ fn main() 
      )
  }
  
+ #[test]
+ fn doctest_convert_let_else_to_match() {
+     check_doc_test(
+         "convert_let_else_to_match",
+         r#####"
+ fn main() {
+     let Ok(mut x) = f() else$0 { return };
+ }
+ "#####,
+         r#####"
+ fn main() {
+     let mut x = match f() {
+         Ok(x) => x,
+         _ => return,
+     };
+ }
+ "#####,
+     )
+ }
  #[test]
  fn doctest_convert_to_guarded_return() {
      check_doc_test(
@@@ -819,7 -819,6 +839,7 @@@ fn doctest_generate_deref() 
      check_doc_test(
          "generate_deref",
          r#####"
 +//- minicore: deref, deref_mut
  struct A;
  struct B {
     $0a: A
@@@ -831,7 -830,7 +851,7 @@@ struct B 
     a: A
  }
  
 -impl std::ops::Deref for B {
 +impl core::ops::Deref for B {
      type Target = A;
  
      fn deref(&self) -> &Self::Target {
      )
  }
  
 -#[test]
 -fn doctest_generate_derive() {
 -    check_doc_test(
 -        "generate_derive",
 -        r#####"
 -struct Point {
 -    x: u32,
 -    y: u32,$0
 -}
 -"#####,
 -        r#####"
 -#[derive($0)]
 -struct Point {
 -    x: u32,
 -    y: u32,
 -}
 -"#####,
 -    )
 -}
 -
  #[test]
  fn doctest_generate_documentation_template() {
      check_doc_test(
@@@ -1037,6 -1056,8 +1057,6 @@@ struct Person 
  }
  
  impl Person {
 -    /// Get a reference to the person's name.
 -    #[must_use]
      fn $0name(&self) -> &str {
          self.name.as_ref()
      }
@@@ -1060,6 -1081,8 +1080,6 @@@ struct Person 
  }
  
  impl Person {
 -    /// Get a mutable reference to the person's name.
 -    #[must_use]
      fn $0name_mut(&mut self) -> &mut String {
          &mut self.name
      }
@@@ -1157,6 -1180,7 +1177,6 @@@ struct Person 
  }
  
  impl Person {
 -    /// Set the person's name.
      fn set_name(&mut self, name: String) {
          self.name = name;
      }
@@@ -1733,34 -1757,34 +1753,34 @@@ const test: Foo = Foo {foo: 1, bar: 0
  }
  
  #[test]
 -fn doctest_reorder_impl() {
 +fn doctest_reorder_impl_items() {
      check_doc_test(
 -        "reorder_impl",
 +        "reorder_impl_items",
          r#####"
  trait Foo {
 -    fn a() {}
 -    fn b() {}
 -    fn c() {}
 +    type A;
 +    const B: u8;
 +    fn c();
  }
  
  struct Bar;
  $0impl Foo for Bar {
 -    fn b() {}
 +    const B: u8 = 17;
      fn c() {}
 -    fn a() {}
 +    type A = String;
  }
  "#####,
          r#####"
  trait Foo {
 -    fn a() {}
 -    fn b() {}
 -    fn c() {}
 +    type A;
 +    const B: u8;
 +    fn c();
  }
  
  struct Bar;
  impl Foo for Bar {
 -    fn a() {}
 -    fn b() {}
 +    type A = String;
 +    const B: u8 = 17;
      fn c() {}
  }
  "#####,