- #[test]
- fn test_wrap_return_type_option() {
- check_fix(
- r#"
-//- /main.rs crate:main deps:core
-use core::option::Option::{self, Some, None};
-
-fn div(x: i32, y: i32) -> Option<i32> {
- if y == 0 {
- return None;
- }
- x / y$0
-}
-//- /core/lib.rs crate:core
-pub mod result {
- pub enum Result<T, E> { Ok(T), Err(E) }
-}
-pub mod option {
- pub enum Option<T> { Some(T), None }
-}
-"#,
- r#"
-use core::option::Option::{self, Some, None};
-
-fn div(x: i32, y: i32) -> Option<i32> {
- if y == 0 {
- return None;
- }
- Some(x / y)
-}
-"#,
- );
- }
-
- #[test]
- fn test_wrap_return_type() {
- check_fix(
- r#"
-//- /main.rs crate:main deps:core
-use core::result::Result::{self, Ok, Err};
-
-fn div(x: i32, y: i32) -> Result<i32, ()> {
- if y == 0 {
- return Err(());
- }
- x / y$0
-}
-//- /core/lib.rs crate:core
-pub mod result {
- pub enum Result<T, E> { Ok(T), Err(E) }
-}
-pub mod option {
- pub enum Option<T> { Some(T), None }
-}
-"#,
- r#"
-use core::result::Result::{self, Ok, Err};
-
-fn div(x: i32, y: i32) -> Result<i32, ()> {
- if y == 0 {
- return Err(());
- }
- Ok(x / y)
-}
-"#,
- );
- }
-
- #[test]
- fn test_wrap_return_type_handles_generic_functions() {
- check_fix(
- r#"
-//- /main.rs crate:main deps:core
-use core::result::Result::{self, Ok, Err};
-
-fn div<T>(x: T) -> Result<T, i32> {
- if x == 0 {
- return Err(7);
- }
- $0x
-}
-//- /core/lib.rs crate:core
-pub mod result {
- pub enum Result<T, E> { Ok(T), Err(E) }
-}
-pub mod option {
- pub enum Option<T> { Some(T), None }
-}
-"#,
- r#"
-use core::result::Result::{self, Ok, Err};
-
-fn div<T>(x: T) -> Result<T, i32> {
- if x == 0 {
- return Err(7);
- }
- Ok(x)
-}
-"#,
- );
- }
-
- #[test]
- fn test_wrap_return_type_handles_type_aliases() {
- check_fix(
- r#"
-//- /main.rs crate:main deps:core
-use core::result::Result::{self, Ok, Err};
-
-type MyResult<T> = Result<T, ()>;
-
-fn div(x: i32, y: i32) -> MyResult<i32> {
- if y == 0 {
- return Err(());
- }
- x $0/ y
-}
-//- /core/lib.rs crate:core
-pub mod result {
- pub enum Result<T, E> { Ok(T), Err(E) }
-}
-pub mod option {
- pub enum Option<T> { Some(T), None }
-}
-"#,
- r#"
-use core::result::Result::{self, Ok, Err};
-
-type MyResult<T> = Result<T, ()>;
-
-fn div(x: i32, y: i32) -> MyResult<i32> {
- if y == 0 {
- return Err(());
- }
- Ok(x / y)
-}
-"#,
- );
- }
-
- #[test]
- fn test_wrap_return_type_not_applicable_when_expr_type_does_not_match_ok_type() {
- check_no_diagnostics(
- r#"
-//- /main.rs crate:main deps:core
-use core::result::Result::{self, Ok, Err};
-
-fn foo() -> Result<(), i32> { 0 }
-
-//- /core/lib.rs crate:core
-pub mod result {
- pub enum Result<T, E> { Ok(T), Err(E) }
-}
-pub mod option {
- pub enum Option<T> { Some(T), None }
-}
-"#,
- );
- }
-
- #[test]
- fn test_wrap_return_type_not_applicable_when_return_type_is_not_result_or_option() {
- check_no_diagnostics(
- r#"
-//- /main.rs crate:main deps:core
-use core::result::Result::{self, Ok, Err};
-
-enum SomeOtherEnum { Ok(i32), Err(String) }
-
-fn foo() -> SomeOtherEnum { 0 }
-
-//- /core/lib.rs crate:core
-pub mod result {
- pub enum Result<T, E> { Ok(T), Err(E) }
-}
-pub mod option {
- pub enum Option<T> { Some(T), None }
-}
-"#,
- );
- }
-
- #[test]
- fn test_fill_struct_fields_empty() {
- check_fix(
- r#"
-struct TestStruct { one: i32, two: i64 }
-
-fn test_fn() {
- let s = TestStruct {$0};
-}
-"#,
- r#"
-struct TestStruct { one: i32, two: i64 }
-
-fn test_fn() {
- let s = TestStruct { one: (), two: ()};
-}
-"#,
- );
- }
-
- #[test]
- fn test_fill_struct_fields_self() {
- check_fix(
- r#"
-struct TestStruct { one: i32 }
-
-impl TestStruct {
- fn test_fn() { let s = Self {$0}; }
-}
-"#,
- r#"
-struct TestStruct { one: i32 }
-
-impl TestStruct {
- fn test_fn() { let s = Self { one: ()}; }
-}
-"#,
- );
- }
-
- #[test]
- fn test_fill_struct_fields_enum() {
- check_fix(
- r#"
-enum Expr {
- Bin { lhs: Box<Expr>, rhs: Box<Expr> }
-}
-
-impl Expr {
- fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
- Expr::Bin {$0 }
- }
-}
-"#,
- r#"
-enum Expr {
- Bin { lhs: Box<Expr>, rhs: Box<Expr> }
-}
-
-impl Expr {
- fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
- Expr::Bin { lhs: (), rhs: () }
- }
-}
-"#,
- );
- }
-
- #[test]
- fn test_fill_struct_fields_partial() {
- check_fix(
- r#"
-struct TestStruct { one: i32, two: i64 }
-
-fn test_fn() {
- let s = TestStruct{ two: 2$0 };
-}
-"#,
- r"
-struct TestStruct { one: i32, two: i64 }
-
-fn test_fn() {
- let s = TestStruct{ two: 2, one: () };
-}
-",
- );
- }
-
- #[test]
- fn test_fill_struct_fields_no_diagnostic() {
- check_no_diagnostics(
- r"
- struct TestStruct { one: i32, two: i64 }
-
- fn test_fn() {
- let one = 1;
- let s = TestStruct{ one, two: 2 };
- }
- ",
- );
- }
-
- #[test]
- fn test_fill_struct_fields_no_diagnostic_on_spread() {
- check_no_diagnostics(
- r"
- struct TestStruct { one: i32, two: i64 }
-
- fn test_fn() {
- let one = 1;
- let s = TestStruct{ ..a };
- }
- ",
- );
- }
-
- #[test]
- fn test_unresolved_module_diagnostic() {
- check_expect(
- r#"mod foo;"#,
- expect![[r#"
- [
- Diagnostic {
- message: "unresolved module",
- range: 0..8,
- severity: Error,
- fix: Some(
- Assist {
- id: AssistId(
- "create_module",
- QuickFix,
- ),
- label: "Create module",
- group: None,
- target: 0..8,
- source_change: Some(
- SourceChange {
- source_file_edits: {},
- file_system_edits: [
- CreateFile {
- dst: AnchoredPathBuf {
- anchor: FileId(
- 0,
- ),
- path: "foo.rs",
- },
- initial_contents: "",
- },
- ],
- is_snippet: false,
- },
- ),
- },
- ),
- unused: false,
- code: Some(
- DiagnosticCode(
- "unresolved-module",
- ),
- ),
- },
- ]
- "#]],
- );
- }
-
- #[test]
- fn test_unresolved_macro_range() {
- check_expect(
- r#"foo::bar!(92);"#,
- expect![[r#"
- [
- Diagnostic {
- message: "unresolved macro `foo::bar!`",
- range: 5..8,
- severity: Error,
- fix: None,
- unused: false,
- code: Some(
- DiagnosticCode(
- "unresolved-macro-call",
- ),
- ),
- },
- ]
- "#]],
- );