Skip to content

Suppress needless_late_init on if-let and long if/match expr #8613

@dhardy

Description

@dhardy

Summary

According to the lint docs, the sole justification for this lint is to reduce repetitive code. Yes, the examples in the docs look better as suggested, but others don't.

Slightly wordy but clean code:

        let attr_widget;
        if let Some(i) = index {
            let attr = attrs.remove(i);
            let (_, tokens) = parse_attr_group(attr.tokens)?;
            attr_widget = syn::parse2(tokens)?;
        } else {
            attr_widget = Default::default();
        }

Shorter code, but first line contains two = tokens (ugly):

        let attr_widget = if let Some(i) = index {
            let attr = attrs.remove(i);
            let (_, tokens) = parse_attr_group(attr.tokens)?;
            syn::parse2(tokens)?
        } else {
            Default::default()
        };

It's easy to imagine worse examples:

let foo = if let Some(bar) = thing.iter().find(|t| t.is_good()) {
    ...
} else {
    ...
};

Suggestion: suppress lint when first line of if / match is long or includes an if let binding.

Lint Name

needless_late_init

Reproducer

No response

Version

No response

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions