Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sway Ref: Style guide unused variables #4660

Merged
merged 6 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
- [Pattern Matching](./documentation/language/style-guide/pattern-matching.md)
- [Comments](./documentation/language/style-guide/comments.md)
- [Getter Functions](./documentation/language/style-guide/getters.md)
- [Unused Variables](./documentation/language/style-guide/unused-variables.md)

## Common Operations

Expand Down
109 changes: 64 additions & 45 deletions docs/reference/src/code/Forc.lock
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
[[package]]
name = 'annotation_style'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'annotations'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'arrays'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'assertions'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'asset_operations'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'associated-consts'
Expand All @@ -30,30 +30,30 @@ source = 'member'
[[package]]
name = 'booleans'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'bytes'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'call'
source = 'member'
dependencies = [
'contract_interface',
'std',
'std path+from-root-0F28276288D2432C',
]

[[package]]
name = 'call_data'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'comments'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'constants'
Expand All @@ -62,184 +62,203 @@ source = 'member'
[[package]]
name = 'contract_interface'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'control_flow'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'core'
source = 'path+from-root-0F28276288D2432C'

[[package]]
name = 'core'
source = 'path+from-root-D62A8C57DD8D2801'

[[package]]
name = 'counter'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'empty_storage_init'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'enum-advanced'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'enum_style'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'enums'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'fizzbuzz'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'functions'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'getters'
source = 'member'
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'hashing'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'interface'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'letter_casing'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'logging'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'my_lib'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'my_library'
source = 'member'
dependencies = [
'my_other_library',
'std',
'std path+from-root-0F28276288D2432C',
]

[[package]]
name = 'my_other_library'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'namespace'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'numerics'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'ownership'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'reading_writing_to_storage'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'simple_predicate'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'simple_script'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'std'
source = 'git+https://github.com/fuellabs/sway?tag=v0.40.1#48104d0bde0d343154a5bc39a310092532883235'
Braqzen marked this conversation as resolved.
Show resolved Hide resolved
dependencies = ['core path+from-root-D62A8C57DD8D2801']

[[package]]
name = 'std'
source = 'path+from-root-0F28276288D2432C'
dependencies = ['core']
dependencies = ['core path+from-root-0F28276288D2432C']

[[package]]
name = 'storage_init'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'storage_map'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'storage_vec'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'store_get'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'string_issue'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'strings'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'struct_shorthand'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'structs'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'tuples'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'unused_variables'
source = 'member'
dependencies = ['std git+https://github.com/fuellabs/sway?tag=v0.40.1#48104d0bde0d343154a5bc39a310092532883235']

[[package]]
name = 'variables'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']

[[package]]
name = 'wallet'
source = 'member'
dependencies = [
'interface',
'std',
'std path+from-root-0F28276288D2432C',
]

[[package]]
name = 'wallet_example'
source = 'member'
dependencies = ['std']
dependencies = ['std path+from-root-0F28276288D2432C']
2 changes: 2 additions & 0 deletions docs/reference/src/code/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ members = [
"language/program-types/scripts/simple_script",
"language/style-guide/annotation_style",
"language/style-guide/enum_style",
"language/style-guide/getters",
"language/style-guide/letter_casing",
"language/style-guide/struct_shorthand",
"language/style-guide/unused_variables",
"language/traits/associated-consts",
"language/variables",
"misc/advanced-concepts/enum_advanced",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "lib.sw"
license = "Apache-2.0"
name = "unused_variables"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
library;

#[allow(dead_code)]
fn call() -> (u64, u64) {
// Random data for demonstration in the subsequent functions
(1, 2)
}

#[allow(dead_code)]
// ANCHOR: unused_variable
fn unused_variable() -> u64 {
let (timestamp, deposit_amount) = call();

deposit_amount
}
// ANCHOR_END: unused_variable

#[allow(dead_code)]
// ANCHOR: named_unused_variable
fn named_unused_variable() -> u64 {
let (_timestamp, deposit_amount) = call();

deposit_amount
}
// ANCHOR_END: named_unused_variable

#[allow(dead_code)]
// ANCHOR: nameless_variable
fn nameless_variable() -> u64 {
let (_, deposit_amount) = call();

deposit_amount
}
// ANCHOR_END: nameless_variable
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Unused Variables

A good practice is naming variables appropriately; however, variables may be unused at times such as the `timestamp` from the `call()`.

```sway
{{#include ../../../code/language/style-guide/unused_variables/src/lib.sw:unused_variable}}
```

## Named

We may preserve the name to provide context to the reader by prepending the variable with `_`.

```sway
{{#include ../../../code/language/style-guide/unused_variables/src/lib.sw:named_unused_variable}}
```

## Nameless

We may discard the context and the value by assigning it to `_`.

```sway
{{#include ../../../code/language/style-guide/unused_variables/src/lib.sw:nameless_variable}}
```