diff options
author | Aleksey Kladov <[email protected]> | 2021-06-14 17:32:39 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-06-14 17:45:39 +0100 |
commit | 4768e5fb23c058eba90f0a1dcd6e9d5c0ecdee1b (patch) | |
tree | 814f6a8ace5d131b9f704b3f81e8287b36943b79 /crates/ide_diagnostics/src/handlers/mismatched_arg_count.rs | |
parent | 94f7b63522cb7464a853c74a8431587db6434b12 (diff) |
internal: document diagnostics crate
Diffstat (limited to 'crates/ide_diagnostics/src/handlers/mismatched_arg_count.rs')
-rw-r--r-- | crates/ide_diagnostics/src/handlers/mismatched_arg_count.rs | 272 |
1 files changed, 272 insertions, 0 deletions
diff --git a/crates/ide_diagnostics/src/handlers/mismatched_arg_count.rs b/crates/ide_diagnostics/src/handlers/mismatched_arg_count.rs new file mode 100644 index 000000000..ce313b2cc --- /dev/null +++ b/crates/ide_diagnostics/src/handlers/mismatched_arg_count.rs | |||
@@ -0,0 +1,272 @@ | |||
1 | use crate::{Diagnostic, DiagnosticsContext}; | ||
2 | |||
3 | // Diagnostic: mismatched-arg-count | ||
4 | // | ||
5 | // This diagnostic is triggered if a function is invoked with an incorrect amount of arguments. | ||
6 | pub(crate) fn mismatched_arg_count( | ||
7 | ctx: &DiagnosticsContext<'_>, | ||
8 | d: &hir::MismatchedArgCount, | ||
9 | ) -> Diagnostic { | ||
10 | let s = if d.expected == 1 { "" } else { "s" }; | ||
11 | let message = format!("expected {} argument{}, found {}", d.expected, s, d.found); | ||
12 | Diagnostic::new( | ||
13 | "mismatched-arg-count", | ||
14 | message, | ||
15 | ctx.sema.diagnostics_display_range(d.call_expr.clone().map(|it| it.into())).range, | ||
16 | ) | ||
17 | } | ||
18 | |||
19 | #[cfg(test)] | ||
20 | mod tests { | ||
21 | use crate::tests::check_diagnostics; | ||
22 | |||
23 | #[test] | ||
24 | fn simple_free_fn_zero() { | ||
25 | check_diagnostics( | ||
26 | r#" | ||
27 | fn zero() {} | ||
28 | fn f() { zero(1); } | ||
29 | //^^^^^^^ expected 0 arguments, found 1 | ||
30 | "#, | ||
31 | ); | ||
32 | |||
33 | check_diagnostics( | ||
34 | r#" | ||
35 | fn zero() {} | ||
36 | fn f() { zero(); } | ||
37 | "#, | ||
38 | ); | ||
39 | } | ||
40 | |||
41 | #[test] | ||
42 | fn simple_free_fn_one() { | ||
43 | check_diagnostics( | ||
44 | r#" | ||
45 | fn one(arg: u8) {} | ||
46 | fn f() { one(); } | ||
47 | //^^^^^ expected 1 argument, found 0 | ||
48 | "#, | ||
49 | ); | ||
50 | |||
51 | check_diagnostics( | ||
52 | r#" | ||
53 | fn one(arg: u8) {} | ||
54 | fn f() { one(1); } | ||
55 | "#, | ||
56 | ); | ||
57 | } | ||
58 | |||
59 | #[test] | ||
60 | fn method_as_fn() { | ||
61 | check_diagnostics( | ||
62 | r#" | ||
63 | struct S; | ||
64 | impl S { fn method(&self) {} } | ||
65 | |||
66 | fn f() { | ||
67 | S::method(); | ||
68 | } //^^^^^^^^^^^ expected 1 argument, found 0 | ||
69 | "#, | ||
70 | ); | ||
71 | |||
72 | check_diagnostics( | ||
73 | r#" | ||
74 | struct S; | ||
75 | impl S { fn method(&self) {} } | ||
76 | |||
77 | fn f() { | ||
78 | S::method(&S); | ||
79 | S.method(); | ||
80 | } | ||
81 | "#, | ||
82 | ); | ||
83 | } | ||
84 | |||
85 | #[test] | ||
86 | fn method_with_arg() { | ||
87 | check_diagnostics( | ||
88 | r#" | ||
89 | struct S; | ||
90 | impl S { fn method(&self, arg: u8) {} } | ||
91 | |||
92 | fn f() { | ||
93 | S.method(); | ||
94 | } //^^^^^^^^^^ expected 1 argument, found 0 | ||
95 | "#, | ||
96 | ); | ||
97 | |||
98 | check_diagnostics( | ||
99 | r#" | ||
100 | struct S; | ||
101 | impl S { fn method(&self, arg: u8) {} } | ||
102 | |||
103 | fn f() { | ||
104 | S::method(&S, 0); | ||
105 | S.method(1); | ||
106 | } | ||
107 | "#, | ||
108 | ); | ||
109 | } | ||
110 | |||
111 | #[test] | ||
112 | fn method_unknown_receiver() { | ||
113 | // note: this is incorrect code, so there might be errors on this in the | ||
114 | // future, but we shouldn't emit an argument count diagnostic here | ||
115 | check_diagnostics( | ||
116 | r#" | ||
117 | trait Foo { fn method(&self, arg: usize) {} } | ||
118 | |||
119 | fn f() { | ||
120 | let x; | ||
121 | x.method(); | ||
122 | } | ||
123 | "#, | ||
124 | ); | ||
125 | } | ||
126 | |||
127 | #[test] | ||
128 | fn tuple_struct() { | ||
129 | check_diagnostics( | ||
130 | r#" | ||
131 | struct Tup(u8, u16); | ||
132 | fn f() { | ||
133 | Tup(0); | ||
134 | } //^^^^^^ expected 2 arguments, found 1 | ||
135 | "#, | ||
136 | ) | ||
137 | } | ||
138 | |||
139 | #[test] | ||
140 | fn enum_variant() { | ||
141 | check_diagnostics( | ||
142 | r#" | ||
143 | enum En { Variant(u8, u16), } | ||
144 | fn f() { | ||
145 | En::Variant(0); | ||
146 | } //^^^^^^^^^^^^^^ expected 2 arguments, found 1 | ||
147 | "#, | ||
148 | ) | ||
149 | } | ||
150 | |||
151 | #[test] | ||
152 | fn enum_variant_type_macro() { | ||
153 | check_diagnostics( | ||
154 | r#" | ||
155 | macro_rules! Type { | ||
156 | () => { u32 }; | ||
157 | } | ||
158 | enum Foo { | ||
159 | Bar(Type![]) | ||
160 | } | ||
161 | impl Foo { | ||
162 | fn new() { | ||
163 | Foo::Bar(0); | ||
164 | Foo::Bar(0, 1); | ||
165 | //^^^^^^^^^^^^^^ expected 1 argument, found 2 | ||
166 | Foo::Bar(); | ||
167 | //^^^^^^^^^^ expected 1 argument, found 0 | ||
168 | } | ||
169 | } | ||
170 | "#, | ||
171 | ); | ||
172 | } | ||
173 | |||
174 | #[test] | ||
175 | fn varargs() { | ||
176 | check_diagnostics( | ||
177 | r#" | ||
178 | extern "C" { | ||
179 | fn fixed(fixed: u8); | ||
180 | fn varargs(fixed: u8, ...); | ||
181 | fn varargs2(...); | ||
182 | } | ||
183 | |||
184 | fn f() { | ||
185 | unsafe { | ||
186 | fixed(0); | ||
187 | fixed(0, 1); | ||
188 | //^^^^^^^^^^^ expected 1 argument, found 2 | ||
189 | varargs(0); | ||
190 | varargs(0, 1); | ||
191 | varargs2(); | ||
192 | varargs2(0); | ||
193 | varargs2(0, 1); | ||
194 | } | ||
195 | } | ||
196 | "#, | ||
197 | ) | ||
198 | } | ||
199 | |||
200 | #[test] | ||
201 | fn arg_count_lambda() { | ||
202 | check_diagnostics( | ||
203 | r#" | ||
204 | fn main() { | ||
205 | let f = |()| (); | ||
206 | f(); | ||
207 | //^^^ expected 1 argument, found 0 | ||
208 | f(()); | ||
209 | f((), ()); | ||
210 | //^^^^^^^^^ expected 1 argument, found 2 | ||
211 | } | ||
212 | "#, | ||
213 | ) | ||
214 | } | ||
215 | |||
216 | #[test] | ||
217 | fn cfgd_out_call_arguments() { | ||
218 | check_diagnostics( | ||
219 | r#" | ||
220 | struct C(#[cfg(FALSE)] ()); | ||
221 | impl C { | ||
222 | fn new() -> Self { | ||
223 | Self( | ||
224 | #[cfg(FALSE)] | ||
225 | (), | ||
226 | ) | ||
227 | } | ||
228 | |||
229 | fn method(&self) {} | ||
230 | } | ||
231 | |||
232 | fn main() { | ||
233 | C::new().method(#[cfg(FALSE)] 0); | ||
234 | } | ||
235 | "#, | ||
236 | ); | ||
237 | } | ||
238 | |||
239 | #[test] | ||
240 | fn cfgd_out_fn_params() { | ||
241 | check_diagnostics( | ||
242 | r#" | ||
243 | fn foo(#[cfg(NEVER)] x: ()) {} | ||
244 | |||
245 | struct S; | ||
246 | |||
247 | impl S { | ||
248 | fn method(#[cfg(NEVER)] self) {} | ||
249 | fn method2(#[cfg(NEVER)] self, arg: u8) {} | ||
250 | fn method3(self, #[cfg(NEVER)] arg: u8) {} | ||
251 | } | ||
252 | |||
253 | extern "C" { | ||
254 | fn fixed(fixed: u8, #[cfg(NEVER)] ...); | ||
255 | fn varargs(#[cfg(not(NEVER))] ...); | ||
256 | } | ||
257 | |||
258 | fn main() { | ||
259 | foo(); | ||
260 | S::method(); | ||
261 | S::method2(0); | ||
262 | S::method3(S); | ||
263 | S.method3(); | ||
264 | unsafe { | ||
265 | fixed(0); | ||
266 | varargs(1, 2, 3); | ||
267 | } | ||
268 | } | ||
269 | "#, | ||
270 | ) | ||
271 | } | ||
272 | } | ||