Skip to content

"Implement default member" assist on trait messes up on pattern #20215

@antonilol

Description

@antonilol

rust-analyzer version: 0.3.2533-standalone

rustc version: rustc 1.88.0 (6b00bc388 2025-06-23)

editor or extension: Code OSS (like vscode)

relevant settings: None

code snippet to reproduce:

struct MyRange;

impl core::ops::RangeBounds<usize> for MyRange {
    fn start_bound(&self) -> core::ops::Bound<&usize> {
        todo!()
    }

    fn end_bound(&self) -> core::ops::Bound<&usize> {
        todo!()
    }
}

Click on impl and use the "Implement default member" assist.

The resulting code looks like this:

struct MyRange;

impl core::ops::RangeBounds<usize> for MyRange {
    fn start_bound(&self) -> core::ops::Bound<&usize> {
        todo!()
    }

    fn end_bound(&self) -> core::ops::Bound<&usize> {
        todo!()
    }
    
    fn contains<U>(&self, item: &U) -> bool
    where
        usize: PartialOrd<U>,
        U: ?Sized + PartialOrd<usize>,
    {
        (match self.start_bound() {
            std::ops::Bound::Included(start) => start <= item,
            std::ops::Bound::Excluded(start) => start < item,
            Unbounded => true,
        }) && (match self.end_bound() {
            std::ops::Bound::Included(end) => item <= end,
            std::ops::Bound::Excluded(end) => item < end,
            Unbounded => true,
        })
    }
    
    fn is_empty(&self) -> bool
    where
        usize: PartialOrd,
    {
        !match (self.start_bound(), self.end_bound()) {
            (Unbounded, _) | (_, Unbounded) => true,
            (std::ops::Bound::Included(start), std::ops::Bound::Excluded(end))
            | (std::ops::Bound::Excluded(start), std::ops::Bound::Included(end))
            | (std::ops::Bound::Excluded(start), std::ops::Bound::Excluded(end)) => start < end,
            (std::ops::Bound::Included(start), std::ops::Bound::Included(end)) => start <= end,
        }
    }
}

This fails to compile because is_empty is unstable and because the lint bindings_with_variant_name is deny by default, it triggers on Unbounded which should be std::ops::Bound::Unbounded.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-assistsC-bugCategory: bugE-has-instructionsIssue has some instructions and pointers to code to get started

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions