← All lints

none_check_unwrap

L0508Idiomswarn

Check for None, then unwrap

x != None and .. x!: the check and the unwrap say one thing twice

Flagged
fn hit(p: ?Int, v: Int) -> Bool = p != None and v == p!
print(hit(None, 1))
Write instead
fn hit(p: ?Int, v: Int) -> Bool = p == Some(v)
print(yes(hit(None, 1)))

Why

x != None and .. x! .. tests the option and then unwraps it: the same fact twice, and the ! stays if the test is edited away. Bind the value once with if let v = x:, or compare the option itself (x == Some(e)).

The practice it serves

A hand-written pattern the language or the standard library already says in one step. Family: O. Optionals and sentinels.

Not flagged

A near miss: close to the pattern, but not it.

Near miss
fn hit(p: ?Int, v: Int) -> Bool = p != None and v > 0
print(yes(hit(None, 1)))

Turn it off

For one block, wrap it in with lint(none_check_unwrap: allow):. For a project, add none_check_unwrap = "allow" under [lints] in cp.toml.

Generated from spec/lints/none_check_unwrap, the same record the linter is built from.