// Type of expr should be iterable.
return it.in_token().is_none() ||
it.iterable()
- .and_then(|iterable_expr|sema.type_of_expr(&iterable_expr))
- .map(|iterable_ty| iterable_ty.is_unknown() || iterable_ty.is_unit())
- .unwrap_or(true)
+ .and_then(|iterable_expr| sema.type_of_expr(&iterable_expr))
+ .map_or(true, |iterable_ty| iterable_ty.is_unknown() || iterable_ty.is_unit())
},
_ => (),
}
// hide when:
// - the parameter name is a suffix of the function's name
// - the argument is an enum whose name is equal to the parameter
- // - exact argument<->parameter match(ignoring leading underscore) or argument is a prefix/suffix
- // of parameter with _ splitting it off
+ // - exact argument<->parameter match(ignoring leading underscore) or parameter is a prefix/suffix
+ // of argument with _ splitting it off
// - param starts with `ra_fixture`
// - param is a well known name in an unary function
}
fn is_argument_similar_to_param_name(argument: &ast::Expr, param_name: &str) -> bool {
- match get_string_representation(argument) {
- None => false,
- Some(argument) => {
- let mut res = false;
- if let Some(first) = argument.bytes().skip_while(|&c| c == b'_').position(|c| c == b'_')
- {
- res |= param_name == argument[..first].trim_start_matches('_');
- }
- if let Some(last) =
- argument.bytes().rev().skip_while(|&c| c == b'_').position(|c| c == b'_')
- {
- res |= param_name == argument[last..].trim_end_matches('_');
- }
- res |= argument == param_name;
- res
- }
+ // check whether param_name and argument are the same or
+ // whether param_name is a prefix/suffix of argument(split at `_`)
+ let argument = match get_string_representation(argument) {
+ Some(argument) => argument,
+ None => return false,
+ };
+
+ let param_name = param_name.trim_start_matches('_');
+ let argument = argument.trim_start_matches('_');
+ if argument.strip_prefix(param_name).map_or(false, |s| s.starts_with('_')) {
+ return true;
+ }
+ if argument.strip_suffix(param_name).map_or(false, |s| s.ends_with('_')) {
+ return true;
}
+ argument == param_name
}
/// Hide the parameter name of an unary function if it is a `_` - prefixed suffix of the function's name, or equal.
check_with_config(TEST_CONFIG, ra_fixture);
}
+ fn check_params(ra_fixture: &str) {
+ check_with_config(
+ InlayHintsConfig {
+ parameter_hints: true,
+ type_hints: false,
+ chaining_hints: false,
+ max_length: None,
+ },
+ ra_fixture,
+ );
+ }
+
+ fn check_types(ra_fixture: &str) {
+ check_with_config(
+ InlayHintsConfig {
+ parameter_hints: false,
+ type_hints: true,
+ chaining_hints: false,
+ max_length: None,
+ },
+ ra_fixture,
+ );
+ }
+
+ fn check_chains(ra_fixture: &str) {
+ check_with_config(
+ InlayHintsConfig {
+ parameter_hints: false,
+ type_hints: false,
+ chaining_hints: true,
+ max_length: None,
+ },
+ ra_fixture,
+ );
+ }
+
fn check_with_config(config: InlayHintsConfig, ra_fixture: &str) {
let ra_fixture =
format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE);
}
#[test]
- fn param_hints_only() {
+ fn hints_disabled() {
check_with_config(
InlayHintsConfig {
- parameter_hints: true,
type_hints: false,
+ parameter_hints: false,
chaining_hints: false,
max_length: None,
},
r#"
fn foo(a: i32, b: i32) -> i32 { a + b }
+fn main() {
+ let _x = foo(4, 4);
+}"#,
+ );
+ }
+
+ #[test]
+ fn param_hints_only() {
+ check_params(
+ r#"
+fn foo(a: i32, b: i32) -> i32 { a + b }
fn main() {
let _x = foo(
4,
#[test]
fn param_name_similar_to_fn_name_still_hints() {
- check_with_config(
- InlayHintsConfig {
- parameter_hints: true,
- type_hints: false,
- chaining_hints: false,
- max_length: None,
- },
+ check_params(
r#"
fn max(x: i32, y: i32) -> i32 { x + y }
fn main() {
#[test]
fn param_name_similar_to_fn_name() {
- check_with_config(
- InlayHintsConfig {
- parameter_hints: true,
- type_hints: false,
- chaining_hints: false,
- max_length: None,
- },
+ check_params(
r#"
fn param_with_underscore(with_underscore: i32) -> i32 { with_underscore }
fn main() {
#[test]
fn param_name_same_as_fn_name() {
- check_with_config(
- InlayHintsConfig {
- parameter_hints: true,
- type_hints: false,
- chaining_hints: false,
- max_length: None,
- },
+ check_params(
r#"
fn foo(foo: i32) -> i32 { foo }
fn main() {
#[test]
fn never_hide_param_when_multiple_params() {
- check_with_config(
- InlayHintsConfig {
- parameter_hints: true,
- type_hints: false,
- chaining_hints: false,
- max_length: None,
- },
+ check_params(
r#"
fn foo(bar: i32, baz: i32) -> i32 { bar + baz }
fn main() {
}
#[test]
- fn hints_disabled() {
- check_with_config(
- InlayHintsConfig {
- type_hints: false,
- parameter_hints: false,
- chaining_hints: false,
- max_length: None,
- },
+ fn hide_param_hints_for_clones() {
+ check_params(
r#"
-fn foo(a: i32, b: i32) -> i32 { a + b }
+fn foo(bar: i32, baz: String, qux: f32) {}
+
fn main() {
- let _x = foo(4, 4);
-}"#,
+ let bar = 3;
+ let baz = &"baz";
+ let fez = 1.0;
+ foo(bar.clone(), baz.clone(), fez.clone());
+ //^^^^^^^^^^^ qux
+}
+"#,
);
}
#[test]
- fn type_hints_only() {
- check_with_config(
- InlayHintsConfig {
- type_hints: true,
- parameter_hints: false,
- chaining_hints: false,
- max_length: None,
- },
+ fn self_param_hints() {
+ check_params(
r#"
-fn foo(a: i32, b: i32) -> i32 { a + b }
+struct Foo;
+
+impl Foo {
+ fn foo(self: Self) {}
+ fn bar(self: &Self) {}
+}
+
fn main() {
- let _x = foo(4, 4);
- //^^ i32
-}"#,
- );
+ Foo::foo(Foo);
+ //^^^ self
+ Foo::bar(&Foo);
+ //^^^^ self
+}
+"#,
+ )
}
#[test]
- fn default_generic_types_should_not_be_displayed() {
- check(
- r#"
-struct Test<K, T = u8> { k: K, t: T }
-
+ fn param_name_hints_show_for_literals() {
+ check_params(
+ r#"pub fn test(a: i32, b: i32) -> [i32; 2] { [a, b] }
fn main() {
- let zz = Test { t: 23u8, k: 33 };
- //^^ Test<i32>
- let zz_ref = &zz;
- //^^^^^^ &Test<i32>
- let test = || zz;
- //^^^^ || -> Test<i32>
+ test(
+ 0x0fab272b,
+ //^^^^^^^^^^ a
+ 0x0fab272b
+ //^^^^^^^^^^ b
+ );
}"#,
- );
+ )
}
#[test]
- fn let_statement() {
+ fn function_call_parameter_hint() {
check(
r#"
-#[derive(PartialEq)]
enum Option<T> { None, Some(T) }
+use Option::*;
-#[derive(PartialEq)]
-struct Test { a: Option<u32>, b: u8 }
+struct FileId {}
+struct SmolStr {}
-fn main() {
- struct InnerStruct {}
+struct TextRange {}
+struct SyntaxKind {}
+struct NavigationTarget {}
- let test = 54;
- //^^^^ i32
- let test: i32 = 33;
- let mut test = 33;
- //^^^^^^^^ i32
- let _ = 22;
- let test = "test";
- //^^^^ &str
- let test = InnerStruct {};
- //^^^^ InnerStruct
+struct Test {}
- let test = unresolved();
+impl Test {
+ fn method(&self, mut param: i32) -> i32 { param * 2 }
- let test = (42, 'a');
- //^^^^ (i32, char)
- let (a, (b, (c,)) = (2, (3, (9.2,));
- //^ i32 ^ i32 ^ f64
- let &x = &92;
- //^ i32
-}"#,
- );
+ fn from_syntax(
+ file_id: FileId,
+ name: SmolStr,
+ focus_range: Option<TextRange>,
+ full_range: TextRange,
+ kind: SyntaxKind,
+ docs: Option<String>,
+ ) -> NavigationTarget {
+ NavigationTarget {}
}
+}
- #[test]
- fn closure_parameters() {
- check(
- r#"
-fn main() {
- let mut start = 0;
- //^^^^^^^^^ i32
- (0..2).for_each(|increment| { start += increment; });
- //^^^^^^^^^ i32
-
- let multiply =
- //^^^^^^^^ |…| -> i32
- | a, b| a * b
- //^ i32 ^ i32
- ;
-
- let _: i32 = multiply(1, 2);
- let multiply_ref = &multiply;
- //^^^^^^^^^^^^ &|…| -> i32
+fn test_func(mut foo: i32, bar: i32, msg: &str, _: i32, last: i32) -> i32 {
+ foo + bar
+}
- let return_42 = || 42;
- //^^^^^^^^^ || -> i32
+fn main() {
+ let not_literal = 1;
+ //^^^^^^^^^^^ i32
+ let _: i32 = test_func(1, 2, "hello", 3, not_literal);
+ //^ foo ^ bar ^^^^^^^ msg ^^^^^^^^^^^ last
+ let t: Test = Test {};
+ t.method(123);
+ //^^^ param
+ Test::method(&t, 3456);
+ //^^ self ^^^^ param
+ Test::from_syntax(
+ FileId {},
+ //^^^^^^^^^ file_id
+ "impl".into(),
+ //^^^^^^^^^^^^^ name
+ None,
+ //^^^^ focus_range
+ TextRange {},
+ //^^^^^^^^^^^^ full_range
+ SyntaxKind {},
+ //^^^^^^^^^^^^^ kind
+ None,
+ //^^^^ docs
+ );
}"#,
);
}
#[test]
- fn if_expr() {
- check(
+ fn omitted_parameters_hints_heuristics() {
+ check_with_config(
+ InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG },
r#"
-enum Option<T> { None, Some(T) }
-use Option::*;
+fn map(f: i32) {}
+fn filter(predicate: i32) {}
-struct Test { a: Option<u32>, b: u8 }
-
-fn main() {
- let test = Some(Test { a: Some(3), b: 1 });
- //^^^^ Option<Test>
- if let None = &test {};
- if let test = &test {};
- //^^^^ &Option<Test>
- if let Some(test) = &test {};
- //^^^^ &Test
- if let Some(Test { a, b }) = &test {};
- //^ &Option<u32> ^ &u8
- if let Some(Test { a: x, b: y }) = &test {};
- //^ &Option<u32> ^ &u8
- if let Some(Test { a: Some(x), b: y }) = &test {};
- //^ &u32 ^ &u8
- if let Some(Test { a: None, b: y }) = &test {};
- //^ &u8
- if let Some(Test { b: y, .. }) = &test {};
- //^ &u8
- if test == None {}
-}"#,
- );
- }
-
- #[test]
- fn while_expr() {
- check(
- r#"
-enum Option<T> { None, Some(T) }
-use Option::*;
-
-struct Test { a: Option<u32>, b: u8 }
-
-fn main() {
- let test = Some(Test { a: Some(3), b: 1 });
- //^^^^ Option<Test>
- while let Some(Test { a: Some(x), b: y }) = &test {};
- //^ &u32 ^ &u8
-}"#,
- );
- }
-
- #[test]
- fn match_arm_list() {
- check(
- r#"
-enum Option<T> { None, Some(T) }
-use Option::*;
-
-struct Test { a: Option<u32>, b: u8 }
-
-fn main() {
- match Some(Test { a: Some(3), b: 1 }) {
- None => (),
- test => (),
- //^^^^ Option<Test>
- Some(Test { a: Some(x), b: y }) => (),
- //^ u32 ^ u8
- _ => {}
- }
-}"#,
- );
- }
-
- #[test]
- fn hint_truncation() {
- check_with_config(
- InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG },
- r#"
-struct Smol<T>(T);
-
-struct VeryLongOuterName<T>(T);
-
-fn main() {
- let a = Smol(0u32);
- //^ Smol<u32>
- let b = VeryLongOuterName(0usize);
- //^ VeryLongOuterName<…>
- let c = Smol(Smol(0u32))
- //^ Smol<Smol<…>>
-}"#,
- );
- }
-
- #[test]
- fn function_call_parameter_hint() {
- check(
- r#"
-enum Option<T> { None, Some(T) }
-use Option::*;
-
-struct FileId {}
-struct SmolStr {}
-
-struct TextRange {}
-struct SyntaxKind {}
-struct NavigationTarget {}
-
-struct Test {}
-
-impl Test {
- fn method(&self, mut param: i32) -> i32 { param * 2 }
-
- fn from_syntax(
- file_id: FileId,
- name: SmolStr,
- focus_range: Option<TextRange>,
- full_range: TextRange,
- kind: SyntaxKind,
- docs: Option<String>,
- ) -> NavigationTarget {
- NavigationTarget {}
- }
-}
-
-fn test_func(mut foo: i32, bar: i32, msg: &str, _: i32, last: i32) -> i32 {
- foo + bar
-}
-
-fn main() {
- let not_literal = 1;
- //^^^^^^^^^^^ i32
- let _: i32 = test_func(1, 2, "hello", 3, not_literal);
- //^ foo ^ bar ^^^^^^^ msg ^^^^^^^^^^^ last
- let t: Test = Test {};
- t.method(123);
- //^^^ param
- Test::method(&t, 3456);
- //^^ self ^^^^ param
- Test::from_syntax(
- FileId {},
- //^^^^^^^^^ file_id
- "impl".into(),
- //^^^^^^^^^^^^^ name
- None,
- //^^^^ focus_range
- TextRange {},
- //^^^^^^^^^^^^ full_range
- SyntaxKind {},
- //^^^^^^^^^^^^^ kind
- None,
- //^^^^ docs
- );
-}"#,
- );
- }
-
- #[test]
- fn omitted_parameters_hints_heuristics() {
- check_with_config(
- InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG },
- r#"
-fn map(f: i32) {}
-fn filter(predicate: i32) {}
-
-struct TestVarContainer {
- test_var: i32,
-}
+struct TestVarContainer {
+ test_var: i32,
+}
impl TestVarContainer {
fn test_var(&self) -> i32 {
);
}
+ // Type-Hint tests
+
#[test]
- fn unit_structs_have_no_type_hints() {
- check_with_config(
- InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG },
+ fn type_hints_only() {
+ check_types(
r#"
-enum Result<T, E> { Ok(T), Err(E) }
-use Result::*;
-
-struct SyntheticSyntax;
-
+fn foo(a: i32, b: i32) -> i32 { a + b }
fn main() {
- match Ok(()) {
- Ok(_) => (),
- Err(SyntheticSyntax) => (),
- }
+ let _x = foo(4, 4);
+ //^^ i32
}"#,
);
}
#[test]
- fn chaining_hints_ignore_comments() {
- check_expect(
- InlayHintsConfig {
- parameter_hints: false,
- type_hints: false,
- chaining_hints: true,
- max_length: None,
- },
+ fn default_generic_types_should_not_be_displayed() {
+ check(
r#"
-struct A(B);
-impl A { fn into_b(self) -> B { self.0 } }
-struct B(C);
-impl B { fn into_c(self) -> C { self.0 } }
-struct C;
+struct Test<K, T = u8> { k: K, t: T }
fn main() {
- let c = A(B(C))
- .into_b() // This is a comment
- // This is another comment
- .into_c();
-}
-"#,
- expect![[r#"
- [
- InlayHint {
- range: 148..173,
- kind: ChainingHint,
- label: "B",
- },
- InlayHint {
- range: 148..155,
- kind: ChainingHint,
- label: "A",
- },
- ]
- "#]],
+ let zz = Test { t: 23u8, k: 33 };
+ //^^ Test<i32>
+ let zz_ref = &zz;
+ //^^^^^^ &Test<i32>
+ let test = || zz;
+ //^^^^ || -> Test<i32>
+}"#,
);
}
#[test]
- fn chaining_hints_without_newlines() {
- check_with_config(
- InlayHintsConfig {
- parameter_hints: false,
- type_hints: false,
- chaining_hints: true,
- max_length: None,
- },
+ fn shorten_iterators_in_associated_params() {
+ check_types(
r#"
-struct A(B);
-impl A { fn into_b(self) -> B { self.0 } }
-struct B(C);
-impl B { fn into_c(self) -> C { self.0 } }
-struct C;
+use core::iter;
+
+pub struct SomeIter<T> {}
+
+impl<T> SomeIter<T> {
+ pub fn new() -> Self { SomeIter {} }
+ pub fn push(&mut self, t: T) {}
+}
+
+impl<T> Iterator for SomeIter<T> {
+ type Item = T;
+ fn next(&mut self) -> Option<Self::Item> {
+ None
+ }
+}
fn main() {
- let c = A(B(C)).into_b().into_c();
-}"#,
+ let mut some_iter = SomeIter::new();
+ //^^^^^^^^^^^^^ SomeIter<Take<Repeat<i32>>>
+ some_iter.push(iter::repeat(2).take(2));
+ let iter_of_iters = some_iter.take(2);
+ //^^^^^^^^^^^^^ impl Iterator<Item = impl Iterator<Item = i32>>
+}
+"#,
);
}
#[test]
- fn struct_access_chaining_hints() {
- check_expect(
- InlayHintsConfig {
- parameter_hints: false,
- type_hints: false,
- chaining_hints: true,
- max_length: None,
- },
+ fn infer_call_method_return_associated_types_with_generic() {
+ check_types(
r#"
-struct A { pub b: B }
-struct B { pub c: C }
-struct C(pub bool);
-struct D;
+ pub trait Default {
+ fn default() -> Self;
+ }
+ pub trait Foo {
+ type Bar: Default;
+ }
-impl D {
- fn foo(&self) -> i32 { 42 }
+ pub fn quux<T: Foo>() -> T::Bar {
+ let y = Default::default();
+ //^ <T as Foo>::Bar
+
+ y
+ }
+ "#,
+ );
+ }
+
+ #[test]
+ fn fn_hints() {
+ check_types(
+ r#"
+trait Sized {}
+
+fn foo() -> impl Fn() { loop {} }
+fn foo1() -> impl Fn(f64) { loop {} }
+fn foo2() -> impl Fn(f64, f64) { loop {} }
+fn foo3() -> impl Fn(f64, f64) -> u32 { loop {} }
+fn foo4() -> &'static dyn Fn(f64, f64) -> u32 { loop {} }
+fn foo5() -> &'static dyn Fn(&'static dyn Fn(f64, f64) -> u32, f64) -> u32 { loop {} }
+fn foo6() -> impl Fn(f64, f64) -> u32 + Sized { loop {} }
+fn foo7() -> *const (impl Fn(f64, f64) -> u32 + Sized) { loop {} }
+
+fn main() {
+ let foo = foo();
+ // ^^^ impl Fn()
+ let foo = foo1();
+ // ^^^ impl Fn(f64)
+ let foo = foo2();
+ // ^^^ impl Fn(f64, f64)
+ let foo = foo3();
+ // ^^^ impl Fn(f64, f64) -> u32
+ let foo = foo4();
+ // ^^^ &dyn Fn(f64, f64) -> u32
+ let foo = foo5();
+ // ^^^ &dyn Fn(&dyn Fn(f64, f64) -> u32, f64) -> u32
+ let foo = foo6();
+ // ^^^ impl Fn(f64, f64) -> u32 + Sized
+ let foo = foo7();
+ // ^^^ *const (impl Fn(f64, f64) -> u32 + Sized)
}
+"#,
+ )
+ }
+
+ #[test]
+ fn unit_structs_have_no_type_hints() {
+ check_types(
+ r#"
+enum Result<T, E> { Ok(T), Err(E) }
+use Result::*;
+
+struct SyntheticSyntax;
fn main() {
- let x = A { b: B { c: C(true) } }
- .b
- .c
- .0;
- let x = D
- .foo();
+ match Ok(()) {
+ Ok(_) => (),
+ Err(SyntheticSyntax) => (),
+ }
}"#,
- expect![[r#"
- [
- InlayHint {
- range: 144..191,
- kind: ChainingHint,
- label: "C",
- },
- InlayHint {
- range: 144..180,
- kind: ChainingHint,
- label: "B",
- },
- ]
- "#]],
);
}
#[test]
- fn generic_chaining_hints() {
- check_expect(
- InlayHintsConfig {
- parameter_hints: false,
- type_hints: false,
- chaining_hints: true,
- max_length: None,
- },
+ fn let_statement() {
+ check_types(
r#"
-struct A<T>(T);
-struct B<T>(T);
-struct C<T>(T);
-struct X<T,R>(T, R);
+#[derive(PartialEq)]
+enum Option<T> { None, Some(T) }
+
+#[derive(PartialEq)]
+struct Test { a: Option<u32>, b: u8 }
-impl<T> A<T> {
- fn new(t: T) -> Self { A(t) }
- fn into_b(self) -> B<T> { B(self.0) }
-}
-impl<T> B<T> {
- fn into_c(self) -> C<T> { C(self.0) }
-}
fn main() {
- let c = A::new(X(42, true))
- .into_b()
- .into_c();
-}
-"#,
- expect![[r#"
- [
- InlayHint {
- range: 247..284,
- kind: ChainingHint,
- label: "B<X<i32, bool>>",
- },
- InlayHint {
- range: 247..266,
- kind: ChainingHint,
- label: "A<X<i32, bool>>",
- },
- ]
- "#]],
+ struct InnerStruct {}
+
+ let test = 54;
+ //^^^^ i32
+ let test: i32 = 33;
+ let mut test = 33;
+ //^^^^^^^^ i32
+ let _ = 22;
+ let test = "test";
+ //^^^^ &str
+ let test = InnerStruct {};
+ //^^^^ InnerStruct
+
+ let test = unresolved();
+
+ let test = (42, 'a');
+ //^^^^ (i32, char)
+ let (a, (b, (c,)) = (2, (3, (9.2,));
+ //^ i32 ^ i32 ^ f64
+ let &x = &92;
+ //^ i32
+}"#,
+ );
+ }
+
+ #[test]
+ fn if_expr() {
+ check_types(
+ r#"
+enum Option<T> { None, Some(T) }
+use Option::*;
+
+struct Test { a: Option<u32>, b: u8 }
+
+fn main() {
+ let test = Some(Test { a: Some(3), b: 1 });
+ //^^^^ Option<Test>
+ if let None = &test {};
+ if let test = &test {};
+ //^^^^ &Option<Test>
+ if let Some(test) = &test {};
+ //^^^^ &Test
+ if let Some(Test { a, b }) = &test {};
+ //^ &Option<u32> ^ &u8
+ if let Some(Test { a: x, b: y }) = &test {};
+ //^ &Option<u32> ^ &u8
+ if let Some(Test { a: Some(x), b: y }) = &test {};
+ //^ &u32 ^ &u8
+ if let Some(Test { a: None, b: y }) = &test {};
+ //^ &u8
+ if let Some(Test { b: y, .. }) = &test {};
+ //^ &u8
+ if test == None {}
+}"#,
+ );
+ }
+
+ #[test]
+ fn while_expr() {
+ check_types(
+ r#"
+enum Option<T> { None, Some(T) }
+use Option::*;
+
+struct Test { a: Option<u32>, b: u8 }
+
+fn main() {
+ let test = Some(Test { a: Some(3), b: 1 });
+ //^^^^ Option<Test>
+ while let Some(Test { a: Some(x), b: y }) = &test {};
+ //^ &u32 ^ &u8
+}"#,
+ );
+ }
+
+ #[test]
+ fn match_arm_list() {
+ check_types(
+ r#"
+enum Option<T> { None, Some(T) }
+use Option::*;
+
+struct Test { a: Option<u32>, b: u8 }
+
+fn main() {
+ match Some(Test { a: Some(3), b: 1 }) {
+ None => (),
+ test => (),
+ //^^^^ Option<Test>
+ Some(Test { a: Some(x), b: y }) => (),
+ //^ u32 ^ u8
+ _ => {}
+ }
+}"#,
);
}
#[test]
fn incomplete_for_no_hint() {
- check(
+ check_types(
r#"
fn main() {
let data = &[1i32, 2, 3];
#[test]
fn complete_for_hint() {
- check(
+ check_types(
r#"
pub struct Vec<T> {}
#[test]
fn multi_dyn_trait_bounds() {
- check_with_config(
- InlayHintsConfig {
- type_hints: true,
- parameter_hints: false,
- chaining_hints: false,
- max_length: None,
- },
+ check_types(
r#"
pub struct Vec<T> {}
#[test]
fn shorten_iterator_hints() {
- check_with_config(
- InlayHintsConfig {
- parameter_hints: false,
- type_hints: true,
- chaining_hints: false,
- max_length: None,
- },
+ check_types(
r#"
use core::iter;
}
#[test]
- fn shorten_iterator_chaining_hints() {
- check_expect(
- InlayHintsConfig {
- parameter_hints: false,
- type_hints: false,
- chaining_hints: true,
- max_length: None,
- },
+ fn closures() {
+ check(
r#"
-use core::iter;
+fn main() {
+ let mut start = 0;
+ //^^^^^^^^^ i32
+ (0..2).for_each(|increment| { start += increment; });
+ //^^^^^^^^^ i32
-struct MyIter;
+ let multiply =
+ //^^^^^^^^ |…| -> i32
+ | a, b| a * b
+ //^ i32 ^ i32
+ ;
-impl Iterator for MyIter {
- type Item = ();
- fn next(&mut self) -> Option<Self::Item> {
- None
+ let _: i32 = multiply(1, 2);
+ let multiply_ref = &multiply;
+ //^^^^^^^^^^^^ &|…| -> i32
+
+ let return_42 = || 42;
+ //^^^^^^^^^ || -> i32
+}"#,
+ );
}
-}
+
+ #[test]
+ fn hint_truncation() {
+ check_with_config(
+ InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG },
+ r#"
+struct Smol<T>(T);
+
+struct VeryLongOuterName<T>(T);
fn main() {
- let _x = MyIter.by_ref()
- .take(5)
- .by_ref()
- .take(5)
- .by_ref();
+ let a = Smol(0u32);
+ //^ Smol<u32>
+ let b = VeryLongOuterName(0usize);
+ //^ VeryLongOuterName<…>
+ let c = Smol(Smol(0u32))
+ //^ Smol<Smol<…>>
+}"#,
+ );
+ }
+
+ // Chaining hint tests
+
+ #[test]
+ fn chaining_hints_ignore_comments() {
+ check_expect(
+ InlayHintsConfig {
+ parameter_hints: false,
+ type_hints: false,
+ chaining_hints: true,
+ max_length: None,
+ },
+ r#"
+struct A(B);
+impl A { fn into_b(self) -> B { self.0 } }
+struct B(C);
+impl B { fn into_c(self) -> C { self.0 } }
+struct C;
+
+fn main() {
+ let c = A(B(C))
+ .into_b() // This is a comment
+ // This is another comment
+ .into_c();
}
"#,
expect![[r#"
[
InlayHint {
- range: 175..242,
- kind: ChainingHint,
- label: "impl Iterator<Item = ()>",
- },
- InlayHint {
- range: 175..225,
- kind: ChainingHint,
- label: "impl Iterator<Item = ()>",
- },
- InlayHint {
- range: 175..207,
+ range: 148..173,
kind: ChainingHint,
- label: "impl Iterator<Item = ()>",
+ label: "B",
},
InlayHint {
- range: 175..190,
+ range: 148..155,
kind: ChainingHint,
- label: "&mut MyIter",
+ label: "A",
},
]
"#]],
}
#[test]
- fn shorten_iterators_in_associated_params() {
- check_with_config(
+ fn chaining_hints_without_newlines() {
+ check_chains(
+ r#"
+struct A(B);
+impl A { fn into_b(self) -> B { self.0 } }
+struct B(C);
+impl B { fn into_c(self) -> C { self.0 } }
+struct C;
+
+fn main() {
+ let c = A(B(C)).into_b().into_c();
+}"#,
+ );
+ }
+
+ #[test]
+ fn struct_access_chaining_hints() {
+ check_expect(
InlayHintsConfig {
parameter_hints: false,
- type_hints: true,
- chaining_hints: false,
+ type_hints: false,
+ chaining_hints: true,
max_length: None,
},
r#"
-use core::iter;
-
-pub struct SomeIter<T> {}
-
-impl<T> SomeIter<T> {
- pub fn new() -> Self { SomeIter {} }
- pub fn push(&mut self, t: T) {}
-}
+struct A { pub b: B }
+struct B { pub c: C }
+struct C(pub bool);
+struct D;
-impl<T> Iterator for SomeIter<T> {
- type Item = T;
- fn next(&mut self) -> Option<Self::Item> {
- None
- }
+impl D {
+ fn foo(&self) -> i32 { 42 }
}
fn main() {
- let mut some_iter = SomeIter::new();
- //^^^^^^^^^^^^^ SomeIter<Take<Repeat<i32>>>
- some_iter.push(iter::repeat(2).take(2));
- let iter_of_iters = some_iter.take(2);
- //^^^^^^^^^^^^^ impl Iterator<Item = impl Iterator<Item = i32>>
-}
-"#,
+ let x = A { b: B { c: C(true) } }
+ .b
+ .c
+ .0;
+ let x = D
+ .foo();
+}"#,
+ expect![[r#"
+ [
+ InlayHint {
+ range: 144..191,
+ kind: ChainingHint,
+ label: "C",
+ },
+ InlayHint {
+ range: 144..180,
+ kind: ChainingHint,
+ label: "B",
+ },
+ ]
+ "#]],
);
}
#[test]
- fn hide_param_hints_for_clones() {
- check_with_config(
+ fn generic_chaining_hints() {
+ check_expect(
InlayHintsConfig {
- parameter_hints: true,
+ parameter_hints: false,
type_hints: false,
- chaining_hints: false,
+ chaining_hints: true,
max_length: None,
},
r#"
-fn foo(bar: i32, baz: String, qux: f32) {}
+struct A<T>(T);
+struct B<T>(T);
+struct C<T>(T);
+struct X<T,R>(T, R);
+impl<T> A<T> {
+ fn new(t: T) -> Self { A(t) }
+ fn into_b(self) -> B<T> { B(self.0) }
+}
+impl<T> B<T> {
+ fn into_c(self) -> C<T> { C(self.0) }
+}
fn main() {
- let bar = 3;
- let baz = &"baz";
- let fez = 1.0;
- foo(bar.clone(), baz.clone(), fez.clone());
- //^^^^^^^^^^^ qux
+ let c = A::new(X(42, true))
+ .into_b()
+ .into_c();
}
"#,
+ expect![[r#"
+ [
+ InlayHint {
+ range: 247..284,
+ kind: ChainingHint,
+ label: "B<X<i32, bool>>",
+ },
+ InlayHint {
+ range: 247..266,
+ kind: ChainingHint,
+ label: "A<X<i32, bool>>",
+ },
+ ]
+ "#]],
);
}
#[test]
- fn infer_call_method_return_associated_types_with_generic() {
- check(
+ fn shorten_iterator_chaining_hints() {
+ check_expect(
+ InlayHintsConfig {
+ parameter_hints: false,
+ type_hints: false,
+ chaining_hints: true,
+ max_length: None,
+ },
r#"
- pub trait Default {
- fn default() -> Self;
- }
- pub trait Foo {
- type Bar: Default;
- }
+use core::iter;
- pub fn quux<T: Foo>() -> T::Bar {
- let y = Default::default();
- //^ <T as Foo>::Bar
+struct MyIter;
- y
- }
- "#,
- );
+impl Iterator for MyIter {
+ type Item = ();
+ fn next(&mut self) -> Option<Self::Item> {
+ None
}
-
- #[test]
- fn self_param_hints() {
- check(
- r#"
-struct Foo;
-
-impl Foo {
- fn foo(self: Self) {}
- fn bar(self: &Self) {}
-}
-
-fn main() {
- Foo::foo(Foo);
- //^^^ self
- Foo::bar(&Foo);
- //^^^^ self
}
-"#,
- )
- }
-
- #[test]
- fn fn_hints() {
- check(
- r#"
-trait Sized {}
-
-fn foo() -> impl Fn() { loop {} }
-fn foo1() -> impl Fn(f64) { loop {} }
-fn foo2() -> impl Fn(f64, f64) { loop {} }
-fn foo3() -> impl Fn(f64, f64) -> u32 { loop {} }
-fn foo4() -> &'static dyn Fn(f64, f64) -> u32 { loop {} }
-fn foo5() -> &'static dyn Fn(&'static dyn Fn(f64, f64) -> u32, f64) -> u32 { loop {} }
-fn foo6() -> impl Fn(f64, f64) -> u32 + Sized { loop {} }
-fn foo7() -> *const (impl Fn(f64, f64) -> u32 + Sized) { loop {} }
fn main() {
- let foo = foo();
- // ^^^ impl Fn()
- let foo = foo1();
- // ^^^ impl Fn(f64)
- let foo = foo2();
- // ^^^ impl Fn(f64, f64)
- let foo = foo3();
- // ^^^ impl Fn(f64, f64) -> u32
- let foo = foo4();
- // ^^^ &dyn Fn(f64, f64) -> u32
- let foo = foo5();
- // ^^^ &dyn Fn(&dyn Fn(f64, f64) -> u32, f64) -> u32
- let foo = foo6();
- // ^^^ impl Fn(f64, f64) -> u32 + Sized
- let foo = foo7();
- // ^^^ *const (impl Fn(f64, f64) -> u32 + Sized)
+ let _x = MyIter.by_ref()
+ .take(5)
+ .by_ref()
+ .take(5)
+ .by_ref();
}
"#,
- )
- }
-
- #[test]
- fn param_name_hints_show_for_literals() {
- check(
- r#"pub fn test(a: i32, b: i32) -> [i32; 2] { [a, b] }
-fn main() {
- test(
- 0x0fab272b,
- //^^^^^^^^^^ a
- 0x0fab272b
- //^^^^^^^^^^ b
- );
-}"#,
- )
+ expect![[r#"
+ [
+ InlayHint {
+ range: 175..242,
+ kind: ChainingHint,
+ label: "impl Iterator<Item = ()>",
+ },
+ InlayHint {
+ range: 175..225,
+ kind: ChainingHint,
+ label: "impl Iterator<Item = ()>",
+ },
+ InlayHint {
+ range: 175..207,
+ kind: ChainingHint,
+ label: "impl Iterator<Item = ()>",
+ },
+ InlayHint {
+ range: 175..190,
+ kind: ChainingHint,
+ label: "&mut MyIter",
+ },
+ ]
+ "#]],
+ );
}
}