New lint: mutating implicit copy of const #4882
Labels
A-lint
Area: New lints
E-medium
Call for participation: Medium difficulty level problem and requires some initial experience.
L-correctness
Lint: Belongs in the correctness lint group
Constants are instantiated every time they are used, even for non-
Copy
types. This results in behavior that may seem unintuitive at a first glance. The following code looks like it shouldn't compile at all:However, this code is actually valid. Of course, constants can't actually be modified. What really happens is that every usage of
FOO
creates a new temporary-like value that can be freely mutated even thoughFOO
isconst
.Suggest creating a local variable from the constant and mutating the variable instead, or using a
static
with interior mutability if a global state is truly necessary.As a side note, the following code does work. Is it bad style?
The text was updated successfully, but these errors were encountered: