Skip to content

Commit 724685d

Browse files
committed
Check for unused results in a few more places
It appears I pushed my changes a little too soon and forgot about a few additional cases where we need to check and warn about unused call results.
1 parent 327dfbe commit 724685d

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

compiler/src/hir.rs

+13
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ pub(crate) struct ConstantRef {
144144
pub(crate) source: Option<Identifier>,
145145
pub(crate) name: String,
146146
pub(crate) resolved_type: types::TypeRef,
147+
pub(crate) unused: bool,
147148
pub(crate) location: Location,
148149
}
149150

@@ -228,6 +229,7 @@ pub(crate) struct AssignSetter {
228229
pub(crate) name: Identifier,
229230
pub(crate) value: Expression,
230231
pub(crate) location: Location,
232+
pub(crate) unused: bool,
231233
pub(crate) expected_type: types::TypeRef,
232234
}
233235

@@ -2206,6 +2208,7 @@ impl<'a> LowerToHir<'a> {
22062208
source: None,
22072209
name: ARRAY_INTERNAL_NAME.to_string(),
22082210
resolved_type: types::TypeRef::Unknown,
2211+
unused: false,
22092212
location: node.location,
22102213
},
22112214
))),
@@ -2327,6 +2330,8 @@ impl<'a> LowerToHir<'a> {
23272330
match node {
23282331
Expression::Call(c) => c.unused = rem,
23292332
Expression::IdentifierRef(c) => c.unused = rem,
2333+
Expression::ConstantRef(c) => c.unused = rem,
2334+
Expression::AssignSetter(c) => c.unused = rem,
23302335
_ => {}
23312336
}
23322337
}
@@ -2516,6 +2521,7 @@ impl<'a> LowerToHir<'a> {
25162521
source: self.optional_identifier(node.source),
25172522
name: node.name,
25182523
resolved_type: types::TypeRef::Unknown,
2524+
unused: false,
25192525
location: node.location,
25202526
})
25212527
}
@@ -2795,6 +2801,7 @@ impl<'a> LowerToHir<'a> {
27952801
name: self.identifier(node.name),
27962802
value: self.expression(node.value),
27972803
location: node.location,
2804+
unused: false,
27982805
expected_type: types::TypeRef::Unknown,
27992806
})
28002807
}
@@ -2856,6 +2863,7 @@ impl<'a> LowerToHir<'a> {
28562863
location: node.location,
28572864
})),
28582865
location: node.location,
2866+
unused: false,
28592867
expected_type: types::TypeRef::Unknown,
28602868
})
28612869
}
@@ -5620,6 +5628,7 @@ mod tests {
56205628
source: None,
56215629
name: "$Array".to_string(),
56225630
resolved_type: types::TypeRef::Unknown,
5631+
unused: false,
56235632
location: cols(8, 11),
56245633
}
56255634
))),
@@ -5721,6 +5730,7 @@ mod tests {
57215730
source: None,
57225731
name: "$Array".to_string(),
57235732
resolved_type: types::TypeRef::Unknown,
5733+
unused: false,
57245734
location: loc(2, 4, 15, 15),
57255735
}
57265736
))),
@@ -5869,6 +5879,7 @@ mod tests {
58695879
source: None,
58705880
name: "A".to_string(),
58715881
resolved_type: types::TypeRef::Unknown,
5882+
unused: false,
58725883
location: cols(8, 8)
58735884
}))
58745885
);
@@ -6231,6 +6242,7 @@ mod tests {
62316242
location: cols(14, 14)
62326243
})),
62336244
location: cols(8, 14),
6245+
unused: false,
62346246
expected_type: types::TypeRef::Unknown,
62356247
}))
62366248
);
@@ -6296,6 +6308,7 @@ mod tests {
62966308
location: cols(8, 15)
62976309
})),
62986310
location: cols(8, 15),
6311+
unused: false,
62996312
expected_type: types::TypeRef::Unknown,
63006313
}))
63016314
);

compiler/src/type_check/expressions.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,10 @@ impl<'a> CheckMethodBody<'a> {
25592559
type_arguments: call.type_arguments,
25602560
});
25612561

2562+
if node.unused && returns.must_use(self.db(), rec) {
2563+
self.state.diagnostics.unused_result(self.file(), node.location);
2564+
}
2565+
25622566
node.resolved_type = returns;
25632567
node.resolved_type
25642568
}
@@ -3299,6 +3303,10 @@ impl<'a> CheckMethodBody<'a> {
32993303
type_arguments: call.type_arguments,
33003304
});
33013305

3306+
if node.unused && returns.must_use(self.db(), receiver) {
3307+
self.state.diagnostics.unused_result(self.file(), node.location);
3308+
}
3309+
33023310
returns
33033311
}
33043312

std/fixtures/diagnostics/unused_results.inko

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
let ARRAY = [10]
22

3+
type User {
4+
let mut @name: String
5+
6+
fn mut name=(name: String) -> String {
7+
@name := name
8+
}
9+
}
10+
311
fn return_bool -> Bool {
412
true
513
}
@@ -36,6 +44,10 @@ fn return_never -> Never {
3644
panic('oops')
3745
}
3846

47+
fn Constant_method -> Int {
48+
42
49+
}
50+
3951
fn example1 {
4052
return_bool
4153
return_nil
@@ -46,6 +58,7 @@ fn example1 {
4658
return_borrow
4759
return_option
4860
Option.Some(10).as_ref
61+
Constant_method
4962
nil
5063
}
5164

@@ -59,6 +72,7 @@ fn example2 {
5972
return_borrow()
6073
return_option()
6174
Option.Some(10).as_ref()
75+
Constant_method()
6276
nil
6377
}
6478

@@ -71,14 +85,28 @@ fn example4 {
7185
nil
7286
}
7387

74-
# unused_results.inko:42:3 warning(unused-result): the result of this expression is unused
75-
# unused_results.inko:43:3 warning(unused-result): the result of this expression is unused
76-
# unused_results.inko:44:3 warning(unused-result): the result of this expression is unused
77-
# unused_results.inko:45:3 warning(unused-result): the result of this expression is unused
78-
# unused_results.inko:48:3 warning(unused-result): the result of this expression is unused
88+
fn example5 {
89+
10 + 5
90+
nil
91+
}
92+
93+
fn example6(value: User) {
94+
value.name = 'Alice'
95+
nil
96+
}
97+
98+
# unused_results.inko:54:3 warning(unused-result): the result of this expression is unused
7999
# unused_results.inko:55:3 warning(unused-result): the result of this expression is unused
80100
# unused_results.inko:56:3 warning(unused-result): the result of this expression is unused
81101
# unused_results.inko:57:3 warning(unused-result): the result of this expression is unused
82-
# unused_results.inko:58:3 warning(unused-result): the result of this expression is unused
102+
# unused_results.inko:60:3 warning(unused-result): the result of this expression is unused
83103
# unused_results.inko:61:3 warning(unused-result): the result of this expression is unused
104+
# unused_results.inko:68:3 warning(unused-result): the result of this expression is unused
105+
# unused_results.inko:69:3 warning(unused-result): the result of this expression is unused
84106
# unused_results.inko:70:3 warning(unused-result): the result of this expression is unused
107+
# unused_results.inko:71:3 warning(unused-result): the result of this expression is unused
108+
# unused_results.inko:74:3 warning(unused-result): the result of this expression is unused
109+
# unused_results.inko:75:3 warning(unused-result): the result of this expression is unused
110+
# unused_results.inko:84:3 warning(unused-result): the result of this expression is unused
111+
# unused_results.inko:89:3 warning(unused-result): the result of this expression is unused
112+
# unused_results.inko:94:3 warning(unused-result): the result of this expression is unused

std/test/std/test_env.inko

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ fn pub tests(t: mut Tests) {
6767
'env.working_directory=',
6868
child: fn {
6969
let out = Stdout.new
70+
let _ = env.working_directory = env.temporary_directory
7071

71-
env.working_directory = env.temporary_directory
7272
out.write_string(env.working_directory.get.to_string)
7373
},
7474
test: fn (test, process) {

0 commit comments

Comments
 (0)