Skip to main content

Lints

Besides errors, which prevent a program from compiling, the compiler reports warnings about code that works but is likely to be a mistake, or could be written more simply. These are called lints.

Each lint can be configured to one of the following levels:

LevelDescription
allowThe lint isn't reported.
warnThe lint is reported as a warning.
denyThe lint is reported as an error, which fails compilation.
forbidLike deny, but attributes and later flags can't change the level.

You can see every lint and its default level by running rue lints. Many lints come with a suggested fix, which the language server can apply for you as a quick fix.

Lint Groups​

Related lints are also in groups, which can be configured anywhere a lint can:

  • unused contains every unused_* lint.
  • style contains lints for code that works, but could be written more simply, such as unnecessary_parens and collapsible_if.
  • suspicious contains lints for code that's likely to be a mistake, such as self_comparison and unconditional_recursion.

A group sets the level of every lint in it, including the ones that are allowed by default, so style = "warn" also enables negated_condition and shadowed_binding.

Configuring Lints​

Rue.toml​

Levels can be set in the [lints] section of Rue.toml:

[lints]
unused = "deny"
unused_binding = "allow"
unnecessary_cast = "allow"

The [lints] section goes alongside the [compiler] section, which every Rue.toml must have.

Within Rue.toml, a lint's own level takes precedence over the level of its group, regardless of the order they're written in. Here every unused_* lint is denied, except for unused_binding. The exception is a forbidden group, whose lints can't be set to any other level, so unused = "forbid" with unused_binding = "allow" is an error.

Command Line​

Levels can also be overridden for a single rue build or rue test with -A/--allow, -W/--warn, -D/--deny and -F/--forbid, which take precedence over Rue.toml:

rue build -D unused -A unused_import

Among the flags, a lint's own flag takes precedence over its group's, unless the group is forbidden. Otherwise, if a lint is passed more than once, the last flag wins, unless the lint is already forbidden in Rue.toml or by an earlier flag (which is an error).

Attributes​

Levels can also be set in the source code, with an allow, warn, deny or forbid attribute on an item (such as a function, constant, struct, enum, type alias, import or module) or a statement. The attribute applies to everything inside of what it's attached to, and the innermost one takes precedence over any outer attributes and the project's levels, whether it names a lint or a group. An attribute can list multiple lints, separated by commas:

#[deny(unused_binding)]
mod strict {
#[allow(unused_function, unused_parameter)]
fn reserved(value: Int) -> Int {
#[warn(unused_binding)]
let unused = 1;
42
}
}

If an item or statement has more than one attribute for the same lint, the last one wins.

A forbid attribute works like deny, but nothing inside of it can change the lint's level. The same goes for lints forbidden in Rue.toml or on the command line. An attribute or flag that tries to change the level of a forbidden lint has no effect on it, and is reported as an error:

#[forbid(unused_binding)]
fn strict() -> Int {
#[allow(unused_binding)] // Error: Cannot set lint `unused_binding` to `allow`
let unused = 1;
42
}

Expectations​

An expect attribute allows a lint like allow does, but also expects it to be reported inside of what it's attached to. If it never is, the attribute is reported with the unfulfilled_lint_expectation lint, so it doesn't stay around after the code it was written for changes:

fn main() -> Int {
#[expect(unused_binding)]
let unused = 1;
42
}

An expect for a group is fulfilled by any lint in it, and like allow, it can't change a forbidden lint. Expectations aren't checked if there are any errors other than lints, since code that doesn't compile can't be fully linted.

Inner Attributes​

To set a level for a whole file, use an inner attribute (with a !) at the start of the file, before any items. Inner attributes can also go at the start of a mod { ... } body to apply to the whole module, where they come after the attributes on the mod itself, so they take precedence unless those forbid the lint. Attributes on the items inside still override them:

#![allow(unused_function)]

fn reserved() -> Int {
0
}

#[warn(unused_function)]
fn checked() -> Int {
1
}

Naming Conventions​

The naming_convention lint checks that names follow these conventions:

KindConventionExample
Functions, modules, and bindingssnake_casetree_hash
Structs, enums, enum variants, type aliases, and generic typesUpperCamelCaseCreateCoin
ConstantsSCREAMING_SNAKE_CASECREATE_COIN
Parameters and struct fieldssnake_case, SCREAMING_SNAKE_CASE, or Title_Snake_Casepublic_key

Parameters and struct fields can also use SCREAMING_SNAKE_CASE, since curried parameters are conventionally written that way (such as MOD_HASH), or Title_Snake_Case, which is conventionally used for truths (such as Ephemeral_State). A leading underscore isn't considered part of the name.

Unused Code​

Names that start with an underscore are exempt from the unused_* lints. This is useful for parameters and struct fields that are only needed to match the layout of a value:

fn main(_curried_value: Bytes32, amount: Int) -> Int {
amount
}

List of Lints​

Most lints have the same default level for every program. The exception is needless_keccak256_verify, which is allowed by default, but warns by default when the project targets hard fork 2 (with hard_fork_2 = true in Rue.toml, or --hard-fork-2). Before hard fork 2, keccak256_verify is the only way to check a Keccak-256 hash, since keccak256 is an error. After it, the same check can be written as assert keccak256(value) == hash, and the lint suggests that as a fix.

Unused​

LintDefaultDescription
unused_functionwarnFunctions that are never used
unused_constantwarnConstants that are never used
unused_bindingwarnLet bindings that are never used
unused_parameterwarnFunction parameters that are never used
unused_structwarnStructs that are never used
unused_type_aliaswarnType aliases that are never used
unused_enumwarnEnums that are never used
unused_generic_typewarnGeneric type parameters that are never used
unused_importwarnImports that are never used
unused_valuewarnValues returned by expression statements that are discarded
unused_struct_fieldwarnStruct fields that are never read, in structs whose values are only ever read by field
unused_struct_spreadwarnStruct initializers that spread a value (...value) but specify every field that isn't a constant, so nothing is read from it
unused_exportwarnExported items of a module that are only used inside of it

Style​

LintDefaultDescription
empty_genericswarnEmpty generic parameter or argument lists
unnecessary_castwarnCasts between types that are already compatible
unnecessary_guardwarnType guards between types that are already compatible
redundant_union_memberwarnMembers of written union types whose values are all values of another member, so the union is the same without them
unnecessary_pluswarnUnary + operators, which have no effect
unnecessary_returnwarnreturn statements directly in a function body, which can end in an expression instead
bool_comparisonwarnComparisons with true or false, which can use the value or its negation instead
needless_boolwarnif expressions that return true and false, which can use the condition instead
negated_conditionallowif expressions with a negated condition and an else branch, which can swap the branches instead
collapsible_ifwarnelse blocks that only contain an if expression, which can be written as else if
manual_assertwarnif statements that only raise, which can be written as an assert
needless_let_returnwarnBlocks that end by returning a binding declared directly before, which can end in its value instead
redundant_field_nameswarnStruct fields written as name: name, which can use the shorthand name instead
redundant_match_arm_aswarnStruct bindings of match arms written after as, which they don't need
unnecessary_parenswarnParentheses that don't change how an expression is evaluated
naming_conventionwarnNames that don't follow the naming convention for what they name
shadowed_bindingallowBindings and parameters that shadow another binding or parameter with the same name

Suspicious​

LintDefaultDescription
indistinguishable_guardwarnType guards that are always true at runtime
constant_conditionwarnConditions that always evaluate to the same value
double_negationwarnUse of --, which could be confused with a decrement operator
unreachable_codewarnCode after a return, raise or other expression that never finishes, which is never evaluated
inline_if_may_raisewarninline if branches that can raise, even though both are always evaluated
inline_match_may_raisewarninline match arms that can raise, even though every arm is always evaluated
bool_operator_may_raisewarn& and | on Bool whose right operand can raise, even though it's evaluated when the left operand already decides the result
inline_value_may_failwarninline bindings, constants and inline fn arguments that can fail, but aren't always evaluated, so their check can be skipped
self_comparisonwarnComparisons and operations between an expression and itself, which always have the same result
identical_brancheswarnif expressions whose branches are the same, so the condition has no effect
duplicate_conditionwarnConditions in an else if chain, && or || that repeat an earlier one
unreachable_match_armwarnmatch arms that are never taken, since the arms before them handle every value they match
indistinguishable_match_armwarnmatch arms that also handle other members of a union, since they can't be told apart from them at runtime
indistinguishable_union_memberwarnMembers of written union types whose values are all valid values of another member at runtime, such as nil in Int | nil, so a type guard or match can't tell them apart
unconditional_recursionwarnFunctions that call themselves on every path, so they never return
bytes_int_confusionwarnCasts of byte literals to Int whose value is negative or not in canonical form
bracketed_forwarnLists whose only item is a for expression, which is a list already, so the brackets make a list with one list in it
prelude_over_enclosing_itemwarnNames in inline modules that refer to the prelude or a builtin, while an enclosing module has an item with the same name, which inline modules don't see

Other​

LintDefaultDescription
imprecise_narrowingwarnType guards and match arms that leave too many combinations of a struct's fields to narrow it by, so the fields that are left are merged and the struct is wider than it could be
debug_statementallowdebug statements, which shouldn't be left in a release build
needless_keccak256_verifyallowkeccak256_verify calls, which can be written as assert keccak256(value) == hash after hard fork 2 (warns by default with hard_fork_2)
unknown_lintwarnLint names in attributes or Rue.toml that don't refer to a known lint or group
unfulfilled_lint_expectationwarnexpect attributes whose lint is never reported inside of them