mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
Reduce code duplication
This commit is contained in:
parent
7566d7da8a
commit
ea68a1d0c9
1 changed files with 147 additions and 181 deletions
|
@ -324,10 +324,10 @@ mod tests {
|
||||||
/// * a diagnostic is produced
|
/// * a diagnostic is produced
|
||||||
/// * this diagnostic touches the input cursor position
|
/// * this diagnostic touches the input cursor position
|
||||||
/// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied
|
/// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied
|
||||||
fn check_apply_diagnostic_fix_from_position(ra_fixture: &str, after: &str) {
|
fn check_fix(ra_fixture_before: &str, ra_fixture_after: &str) {
|
||||||
let after = trim_indent(after);
|
let after = trim_indent(ra_fixture_after);
|
||||||
|
|
||||||
let (analysis, file_position) = analysis_and_position(ra_fixture);
|
let (analysis, file_position) = analysis_and_position(ra_fixture_before);
|
||||||
let diagnostic = analysis.diagnostics(file_position.file_id).unwrap().pop().unwrap();
|
let diagnostic = analysis.diagnostics(file_position.file_id).unwrap().pop().unwrap();
|
||||||
let mut fix = diagnostic.fix.unwrap();
|
let mut fix = diagnostic.fix.unwrap();
|
||||||
let edit = fix.source_change.source_file_edits.pop().unwrap().edit;
|
let edit = fix.source_change.source_file_edits.pop().unwrap().edit;
|
||||||
|
@ -348,21 +348,6 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_apply_diagnostic_fix(ra_fixture_before: &str, ra_fixture_after: &str) {
|
|
||||||
let ra_fixture_after = &trim_indent(ra_fixture_after);
|
|
||||||
let (analysis, file_id) = single_file(ra_fixture_before);
|
|
||||||
let before = analysis.file_text(file_id).unwrap();
|
|
||||||
let diagnostic = analysis.diagnostics(file_id).unwrap().pop().unwrap();
|
|
||||||
let mut fix = diagnostic.fix.unwrap();
|
|
||||||
let edit = fix.source_change.source_file_edits.pop().unwrap().edit;
|
|
||||||
let actual = {
|
|
||||||
let mut actual = before.to_string();
|
|
||||||
edit.apply(&mut actual);
|
|
||||||
actual
|
|
||||||
};
|
|
||||||
assert_eq_text!(ra_fixture_after, &actual);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Takes a multi-file input fixture with annotated cursor position and checks that no diagnostics
|
/// Takes a multi-file input fixture with annotated cursor position and checks that no diagnostics
|
||||||
/// apply to the file containing the cursor.
|
/// apply to the file containing the cursor.
|
||||||
fn check_no_diagnostic_for_target_file(ra_fixture: &str) {
|
fn check_no_diagnostic_for_target_file(ra_fixture: &str) {
|
||||||
|
@ -379,96 +364,99 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_wrap_return_type() {
|
fn test_wrap_return_type() {
|
||||||
let before = r#"
|
check_fix(
|
||||||
//- /main.rs
|
r#"
|
||||||
use core::result::Result::{self, Ok, Err};
|
//- /main.rs
|
||||||
|
use core::result::Result::{self, Ok, Err};
|
||||||
|
|
||||||
fn div(x: i32, y: i32) -> Result<i32, ()> {
|
fn div(x: i32, y: i32) -> Result<i32, ()> {
|
||||||
if y == 0 {
|
if y == 0 {
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
x / y<|>
|
x / y<|>
|
||||||
}
|
}
|
||||||
//- /core/lib.rs
|
//- /core/lib.rs
|
||||||
pub mod result {
|
pub mod result {
|
||||||
pub enum Result<T, E> { Ok(T), Err(E) }
|
pub enum Result<T, E> { Ok(T), Err(E) }
|
||||||
}
|
}
|
||||||
"#;
|
"#,
|
||||||
let after = r#"
|
r#"
|
||||||
use core::result::Result::{self, Ok, Err};
|
use core::result::Result::{self, Ok, Err};
|
||||||
|
|
||||||
fn div(x: i32, y: i32) -> Result<i32, ()> {
|
fn div(x: i32, y: i32) -> Result<i32, ()> {
|
||||||
if y == 0 {
|
if y == 0 {
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
Ok(x / y)
|
Ok(x / y)
|
||||||
}
|
}
|
||||||
"#;
|
"#,
|
||||||
check_apply_diagnostic_fix_from_position(before, after);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_wrap_return_type_handles_generic_functions() {
|
fn test_wrap_return_type_handles_generic_functions() {
|
||||||
let before = r#"
|
check_fix(
|
||||||
|
r#"
|
||||||
//- /main.rs
|
//- /main.rs
|
||||||
use core::result::Result::{self, Ok, Err};
|
use core::result::Result::{self, Ok, Err};
|
||||||
|
|
||||||
fn div<T>(x: T) -> Result<T, i32> {
|
fn div<T>(x: T) -> Result<T, i32> {
|
||||||
if x == 0 {
|
if x == 0 {
|
||||||
return Err(7);
|
return Err(7);
|
||||||
}
|
}
|
||||||
<|>x
|
<|>x
|
||||||
}
|
}
|
||||||
//- /core/lib.rs
|
//- /core/lib.rs
|
||||||
pub mod result {
|
pub mod result {
|
||||||
pub enum Result<T, E> { Ok(T), Err(E) }
|
pub enum Result<T, E> { Ok(T), Err(E) }
|
||||||
}
|
}
|
||||||
"#;
|
"#,
|
||||||
let after = r#"
|
r#"
|
||||||
use core::result::Result::{self, Ok, Err};
|
use core::result::Result::{self, Ok, Err};
|
||||||
|
|
||||||
fn div<T>(x: T) -> Result<T, i32> {
|
fn div<T>(x: T) -> Result<T, i32> {
|
||||||
if x == 0 {
|
if x == 0 {
|
||||||
return Err(7);
|
return Err(7);
|
||||||
}
|
}
|
||||||
Ok(x)
|
Ok(x)
|
||||||
}
|
}
|
||||||
"#;
|
"#,
|
||||||
check_apply_diagnostic_fix_from_position(before, after);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_wrap_return_type_handles_type_aliases() {
|
fn test_wrap_return_type_handles_type_aliases() {
|
||||||
let before = r#"
|
check_fix(
|
||||||
//- /main.rs
|
r#"
|
||||||
use core::result::Result::{self, Ok, Err};
|
//- /main.rs
|
||||||
|
use core::result::Result::{self, Ok, Err};
|
||||||
|
|
||||||
type MyResult<T> = Result<T, ()>;
|
type MyResult<T> = Result<T, ()>;
|
||||||
|
|
||||||
fn div(x: i32, y: i32) -> MyResult<i32> {
|
fn div(x: i32, y: i32) -> MyResult<i32> {
|
||||||
if y == 0 {
|
if y == 0 {
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
x <|>/ y
|
x <|>/ y
|
||||||
}
|
}
|
||||||
//- /core/lib.rs
|
//- /core/lib.rs
|
||||||
pub mod result {
|
pub mod result {
|
||||||
pub enum Result<T, E> { Ok(T), Err(E) }
|
pub enum Result<T, E> { Ok(T), Err(E) }
|
||||||
}
|
}
|
||||||
"#;
|
"#,
|
||||||
let after = r#"
|
r#"
|
||||||
use core::result::Result::{self, Ok, Err};
|
use core::result::Result::{self, Ok, Err};
|
||||||
|
|
||||||
type MyResult<T> = Result<T, ()>;
|
type MyResult<T> = Result<T, ()>;
|
||||||
|
|
||||||
fn div(x: i32, y: i32) -> MyResult<i32> {
|
fn div(x: i32, y: i32) -> MyResult<i32> {
|
||||||
if y == 0 {
|
if y == 0 {
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
Ok(x / y)
|
Ok(x / y)
|
||||||
}
|
}
|
||||||
"#;
|
"#,
|
||||||
check_apply_diagnostic_fix_from_position(before, after);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -516,116 +504,97 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fill_struct_fields_empty() {
|
fn test_fill_struct_fields_empty() {
|
||||||
let before = r"
|
check_fix(
|
||||||
struct TestStruct {
|
r#"
|
||||||
one: i32,
|
struct TestStruct { one: i32, two: i64 }
|
||||||
two: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_fn() {
|
fn test_fn() {
|
||||||
let s = TestStruct{};
|
let s = TestStruct {<|>};
|
||||||
}
|
}
|
||||||
";
|
"#,
|
||||||
let after = r"
|
r#"
|
||||||
struct TestStruct {
|
struct TestStruct { one: i32, two: i64 }
|
||||||
one: i32,
|
|
||||||
two: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_fn() {
|
fn test_fn() {
|
||||||
let s = TestStruct{ one: (), two: ()};
|
let s = TestStruct { one: (), two: ()};
|
||||||
}
|
}
|
||||||
";
|
"#,
|
||||||
check_apply_diagnostic_fix(before, after);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fill_struct_fields_self() {
|
fn test_fill_struct_fields_self() {
|
||||||
let before = r"
|
check_fix(
|
||||||
struct TestStruct {
|
r#"
|
||||||
one: i32,
|
struct TestStruct { one: i32 }
|
||||||
}
|
|
||||||
|
|
||||||
impl TestStruct {
|
impl TestStruct {
|
||||||
fn test_fn() {
|
fn test_fn() { let s = Self {<|>}; }
|
||||||
let s = Self {};
|
}
|
||||||
}
|
"#,
|
||||||
}
|
r#"
|
||||||
";
|
struct TestStruct { one: i32 }
|
||||||
let after = r"
|
|
||||||
struct TestStruct {
|
|
||||||
one: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TestStruct {
|
impl TestStruct {
|
||||||
fn test_fn() {
|
fn test_fn() { let s = Self { one: ()}; }
|
||||||
let s = Self { one: ()};
|
}
|
||||||
}
|
"#,
|
||||||
}
|
);
|
||||||
";
|
|
||||||
check_apply_diagnostic_fix(before, after);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fill_struct_fields_enum() {
|
fn test_fill_struct_fields_enum() {
|
||||||
let before = r"
|
check_fix(
|
||||||
enum Expr {
|
r#"
|
||||||
Bin { lhs: Box<Expr>, rhs: Box<Expr> }
|
enum Expr {
|
||||||
}
|
Bin { lhs: Box<Expr>, rhs: Box<Expr> }
|
||||||
|
}
|
||||||
|
|
||||||
impl Expr {
|
impl Expr {
|
||||||
fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
|
fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
|
||||||
Expr::Bin { }
|
Expr::Bin {<|> }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
";
|
"#,
|
||||||
let after = r"
|
r#"
|
||||||
enum Expr {
|
enum Expr {
|
||||||
Bin { lhs: Box<Expr>, rhs: Box<Expr> }
|
Bin { lhs: Box<Expr>, rhs: Box<Expr> }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Expr {
|
impl Expr {
|
||||||
fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
|
fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
|
||||||
Expr::Bin { lhs: (), rhs: () }
|
Expr::Bin { lhs: (), rhs: () }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
";
|
"#,
|
||||||
check_apply_diagnostic_fix(before, after);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fill_struct_fields_partial() {
|
fn test_fill_struct_fields_partial() {
|
||||||
let before = r"
|
check_fix(
|
||||||
struct TestStruct {
|
r#"
|
||||||
one: i32,
|
struct TestStruct { one: i32, two: i64 }
|
||||||
two: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_fn() {
|
fn test_fn() {
|
||||||
let s = TestStruct{ two: 2 };
|
let s = TestStruct{ two: 2<|> };
|
||||||
}
|
}
|
||||||
";
|
"#,
|
||||||
let after = r"
|
r"
|
||||||
struct TestStruct {
|
struct TestStruct { one: i32, two: i64 }
|
||||||
one: i32,
|
|
||||||
two: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_fn() {
|
fn test_fn() {
|
||||||
let s = TestStruct{ two: 2, one: () };
|
let s = TestStruct{ two: 2, one: () };
|
||||||
}
|
}
|
||||||
";
|
",
|
||||||
check_apply_diagnostic_fix(before, after);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_fill_struct_fields_no_diagnostic() {
|
fn test_fill_struct_fields_no_diagnostic() {
|
||||||
check_no_diagnostic(
|
check_no_diagnostic(
|
||||||
r"
|
r"
|
||||||
struct TestStruct {
|
struct TestStruct { one: i32, two: i64 }
|
||||||
one: i32,
|
|
||||||
two: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_fn() {
|
fn test_fn() {
|
||||||
let one = 1;
|
let one = 1;
|
||||||
|
@ -639,10 +608,7 @@ mod tests {
|
||||||
fn test_fill_struct_fields_no_diagnostic_on_spread() {
|
fn test_fill_struct_fields_no_diagnostic_on_spread() {
|
||||||
check_no_diagnostic(
|
check_no_diagnostic(
|
||||||
r"
|
r"
|
||||||
struct TestStruct {
|
struct TestStruct { one: i32, two: i64 }
|
||||||
one: i32,
|
|
||||||
two: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_fn() {
|
fn test_fn() {
|
||||||
let one = 1;
|
let one = 1;
|
||||||
|
@ -855,10 +821,10 @@ fn main() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add_field_from_usage() {
|
fn test_add_field_from_usage() {
|
||||||
check_apply_diagnostic_fix(
|
check_fix(
|
||||||
r"
|
r"
|
||||||
fn main() {
|
fn main() {
|
||||||
Foo { bar: 3, baz: false};
|
Foo { bar: 3, baz<|>: false};
|
||||||
}
|
}
|
||||||
struct Foo {
|
struct Foo {
|
||||||
bar: i32
|
bar: i32
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue