Skip to content

Commit 8fde717

Browse files
authored
Add expressive diagnostics for Warning::UnusedReturnValue (#7382)
## Description This PR adds expressive diagnostics for the `UnusedReturnValue` warning that explains how to intentionally ignore a returned value by using `let _ = ...`. The reason for adding the diagnostics was an observed confusion among novice Sway developers in the particular case of using `__dbg` intrinsic as a statement: ```sway __dbg("some debug message"); ``` It was unclear to developers why this usage generates the warning, and also how to remove it. Additionally, the PR fixes some `uninlined_format_args` Clippy warnings. ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
1 parent 608fc52 commit 8fde717

File tree

21 files changed

+156
-74
lines changed

21 files changed

+156
-74
lines changed

forc-plugins/forc-node/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ mod tests {
257257
fn test_human_readable_config() {
258258
let config = Config::local_node();
259259
let human_readable = HumanReadableConfig(&config);
260-
let formatted = format!("{}", human_readable);
260+
let formatted = format!("{human_readable}");
261261
let expected = format!(
262262
r#"Fuel Core Configuration:
263263
GraphQL Address: {}

sway-core/src/language/ty/declaration/const_generic.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ impl MaterializeConstGenerics for TyConstGenericDecl {
5454
.unwrap()
5555
.cast_value_to_u64()
5656
.unwrap(),
57-
"{:?} {:?}",
58-
v,
59-
value
57+
"{v:?} {value:?}",
6058
);
6159
}
6260
None => {

sway-core/src/semantic_analysis/namespace/trait_map.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,24 +236,24 @@ impl DebugWithEngines for TypeRootFilter {
236236
U64 => write!(f, "u64"),
237237
U256 => write!(f, "u256"),
238238
Bool => write!(f, "bool"),
239-
Custom(name) => write!(f, "Custom({})", name),
239+
Custom(name) => write!(f, "Custom({name})"),
240240
B256 => write!(f, "b256"),
241241
Contract => write!(f, "Contract"),
242242
ErrorRecovery => write!(f, "ErrorRecovery"),
243-
Tuple(n) => write!(f, "Tuple(len={})", n),
243+
Tuple(n) => write!(f, "Tuple(len={n})"),
244244
Enum(parsed_id) => {
245-
write!(f, "Enum({:?})", parsed_id)
245+
write!(f, "Enum({parsed_id:?})")
246246
}
247247
Struct(parsed_id) => {
248-
write!(f, "Struct({:?})", parsed_id)
248+
write!(f, "Struct({parsed_id:?})")
249249
}
250-
ContractCaller(abi_name) => write!(f, "ContractCaller({})", abi_name),
250+
ContractCaller(abi_name) => write!(f, "ContractCaller({abi_name})"),
251251
Array => write!(f, "Array"),
252252
RawUntypedPtr => write!(f, "RawUntypedPtr"),
253253
RawUntypedSlice => write!(f, "RawUntypedSlice"),
254254
Ptr => write!(f, "Ptr"),
255255
Slice => write!(f, "Slice"),
256-
TraitType(name) => write!(f, "TraitType({})", name),
256+
TraitType(name) => write!(f, "TraitType({name})"),
257257
}
258258
}
259259
}
@@ -324,14 +324,13 @@ impl DebugWithEngines for TraitMap {
324324

325325
writeln!(
326326
f,
327-
" impl {} for {} [{}]{} {{",
328-
trait_name_str, ty_str, iface_flag, impl_tparams
327+
" impl {trait_name_str} for {ty_str} [{iface_flag}]{impl_tparams} {{"
329328
)?;
330329

331330
for (name, item) in &value.trait_items {
332331
match item {
333332
ResolvedTraitImplItem::Parsed(_p) => {
334-
writeln!(f, " - {}: <parsed>", name)?;
333+
writeln!(f, " - {name}: <parsed>")?;
335334
}
336335
ResolvedTraitImplItem::Typed(ty_item) => {
337336
writeln!(f, " - {}: {:?}", name, engines.help_out(ty_item))?;

sway-error/src/warning.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::{
22
diagnostic::{Code, Diagnostic, Hint, Issue, Reason, ToDiagnostic},
33
formatting::{
4-
did_you_mean_help, num_to_str, sequence_to_list, sequence_to_str, Enclosing, Indent,
4+
did_you_mean_help, first_line, num_to_str, sequence_to_list, sequence_to_str, Enclosing,
5+
Indent,
56
},
67
};
78

@@ -245,7 +246,7 @@ impl fmt::Display for Warning {
245246
},
246247
UnusedReturnValue { r#type } => write!(
247248
f,
248-
"This returns a value of type {type}, which is not assigned to anything and is \
249+
"This returns a value of type \"{type}\", which is not assigned to anything and is \
249250
ignored."
250251
),
251252
SimilarMethodFound { lib, module, name } => write!(
@@ -593,6 +594,25 @@ impl ToDiagnostic for CompileWarning {
593594
},
594595
help: vec![],
595596
},
597+
UnusedReturnValue { r#type } => Diagnostic {
598+
reason: Some(Reason::new(code(1), "Returned value is ignored".to_string())),
599+
issue: Issue::warning(
600+
source_engine,
601+
self.span(),
602+
"This returns a value which is not assigned to anything and is ignored.".to_string(),
603+
),
604+
hints: vec![
605+
Hint::help(
606+
source_engine,
607+
self.span(),
608+
format!("The returned value has type \"{type}\"."),
609+
)
610+
],
611+
help: vec![
612+
"If you want to intentionally ignore the returned value, use `let _ = ...`:".to_string(),
613+
format!("{}let _ = {};", Indent::Single, first_line(self.span.as_str(), true)),
614+
],
615+
},
596616
_ => Diagnostic {
597617
// TODO: Temporarily we use self here to achieve backward compatibility.
598618
// In general, self must not be used and will not be used once we

sway-lib-std/src/clone.sw

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ fn ok_string_array_clone() {
7878
let a = __to_str_array("abc");
7979
let b = a.clone();
8080

81-
let _ = __dbg(a); // TODO __dbg((a, b)) is not working
82-
let _ = __dbg(b);
81+
let _ = __dbg((a, b));
8382

8483
assert(a == __to_str_array("abc"));
8584
assert(b == __to_str_array("abc"));
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
[[package]]
22
name = "insufficient_type_info"
33
source = "member"
4-
dependencies = ["std"]
5-
6-
[[package]]
7-
name = "std"
8-
source = "path+from-root-687D7B6A23A7C9DF"

test/src/e2e_vm_tests/test_programs/should_fail/insufficient_type_info/Forc.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,4 @@ authors = ["Fuel Labs <contact@fuel.sh>"]
33
entry = "main.sw"
44
license = "Apache-2.0"
55
name = "insufficient_type_info"
6-
implicit-std = false
7-
8-
[dependencies]
9-
std = { path = "../../../reduced_std_libs/sway-lib-std-core" }
10-
6+
implicit-std = false

test/src/e2e_vm_tests/test_programs/should_fail/insufficient_type_info/src/main.sw

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
script;
1+
library;
22

33
mod lib;
44

@@ -8,7 +8,7 @@ fn foo<T>() {
88
let x = __size_of::<T>();
99
}
1010

11-
fn main() {
11+
pub fn main() {
1212
foo();
1313

1414
None::<T>;

test/src/e2e_vm_tests/test_programs/should_fail/insufficient_type_info/test.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
category = "fail"
22

3-
# check: $()warning
3+
# check: $()Returned value is ignored
44
# check: $()None::<T>;
5-
# nextln: $()This returns a value of type MyOption<T>, which is not assigned to anything and is ignored.
5+
# nextln: $()This returns a value which is not assigned to anything and is ignored.
6+
# nextln: $()The returned value has type "MyOption<T>".
7+
# check: $()If you want to intentionally ignore the returned value, use `let _ = ...`:
8+
# nextln: $()let _ = None::<T>;
69

710
# check: $()error
811
# check: $()None::<T>;
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
[[package]]
22
name = "break_and_continue_block_ret"
33
source = "member"
4-
dependencies = ["std"]
5-
6-
[[package]]
7-
name = "std"
8-
source = "path+from-root-3052B79D5F74B184"

0 commit comments

Comments
 (0)