diff --git a/docs/reference/src/SUMMARY.md b/docs/reference/src/SUMMARY.md index 9ff0294ae87..dc79f990d61 100644 --- a/docs/reference/src/SUMMARY.md +++ b/docs/reference/src/SUMMARY.md @@ -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) - [Intermediate Variables](./documentation/language/style-guide/intermediate-variables.md) ## Common Operations diff --git a/docs/reference/src/code/Forc.lock b/docs/reference/src/code/Forc.lock index 7c7ad007166..452e710f181 100644 --- a/docs/reference/src/code/Forc.lock +++ b/docs/reference/src/code/Forc.lock @@ -108,6 +108,11 @@ name = 'functions' source = 'member' dependencies = ['std'] +[[package]] +name = 'getters' +source = 'member' +dependencies = ['std'] + [[package]] name = 'hashing' source = 'member' @@ -239,6 +244,11 @@ name = 'tuples' source = 'member' dependencies = ['std'] +[[package]] +name = 'unused_variables' +source = 'member' +dependencies = ['std'] + [[package]] name = 'variables' source = 'member' diff --git a/docs/reference/src/code/Forc.toml b/docs/reference/src/code/Forc.toml index 9ce1f1a38ca..a549d4eec19 100644 --- a/docs/reference/src/code/Forc.toml +++ b/docs/reference/src/code/Forc.toml @@ -26,9 +26,11 @@ 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/pattern_matching", "language/style-guide/struct_shorthand", + "language/style-guide/unused_variables", "language/style-guide/intermediate_variables", "language/traits/associated-consts", "language/variables", diff --git a/docs/reference/src/code/language/style-guide/unused_variables/.gitignore b/docs/reference/src/code/language/style-guide/unused_variables/.gitignore new file mode 100644 index 00000000000..77d3844f58c --- /dev/null +++ b/docs/reference/src/code/language/style-guide/unused_variables/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/docs/reference/src/code/language/style-guide/unused_variables/Forc.toml b/docs/reference/src/code/language/style-guide/unused_variables/Forc.toml new file mode 100644 index 00000000000..8f735f2d65e --- /dev/null +++ b/docs/reference/src/code/language/style-guide/unused_variables/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "lib.sw" +license = "Apache-2.0" +name = "unused_variables" + +[dependencies] +std = { path = "../../../../../../../sway-lib-std" } diff --git a/docs/reference/src/code/language/style-guide/unused_variables/src/lib.sw b/docs/reference/src/code/language/style-guide/unused_variables/src/lib.sw new file mode 100644 index 00000000000..a88af70848d --- /dev/null +++ b/docs/reference/src/code/language/style-guide/unused_variables/src/lib.sw @@ -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 diff --git a/docs/reference/src/documentation/language/style-guide/unused-variables.md b/docs/reference/src/documentation/language/style-guide/unused-variables.md new file mode 100644 index 00000000000..17201f1e545 --- /dev/null +++ b/docs/reference/src/documentation/language/style-guide/unused-variables.md @@ -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}} +```