-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Open
Labels
A-lintArea: New lintsArea: New lintsL-styleLint: Belongs in the style lint groupLint: Belongs in the style lint groupL-suggestionLint: Improving, adding or fixing lint suggestionsLint: Improving, adding or fixing lint suggestions
Description
What it does
In one of my codebases, I regularly used lambdas and immediately called them, to handle nested optional types:
let nested_property: Option<C> = (|| Some(object.a?.b?.c?))();Once try blocks are stabilized, this can be done much cleaner and more idiomatically:
let nested_property: Option<C> = try { object.a?.b?.c? };Another example:
let property = (|| Some(string.get(5..10)?.parse().ok()?))(); // old
let property = try { string.get(5..10)?.parse().ok()? }; // newCategories
- Kind:
clippy::styleorclippy::complexity
What is the advantage of the recommended code over the original code
More idiomatic via usage of the intended language feature instead of abusing closures.
Drawbacks
None.
Example
let nested_property: Option<C> = (|| Some(object.a?.b?.c?))();Could be written as:
let nested_property: Option<C> = try { object.a?.b?.c? };Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-lintArea: New lintsArea: New lintsL-styleLint: Belongs in the style lint groupLint: Belongs in the style lint groupL-suggestionLint: Improving, adding or fixing lint suggestionsLint: Improving, adding or fixing lint suggestions