Skip to content

rustfmt deletes lines instead of detecting a syntax error #4126

Closed
@jbaublitz

Description

@jbaublitz

Code sample

This code sample had an error that I introduced while trying to fix a clippy error.

impl<'a, P> Iterator for NlMessageIter<'a, NlTypeWrapper, P>
where
    P: Nl + Debug,
{
    type Item = Result<Nlmsghdr<NlTypeWrapper, P>, NlError>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(true) = self.next_is_none {
            return None;
        }

        if self.stored.is_empty() {
            match self.socket_ref.recv_all_nl() {
                Ok(mut rn) => {
                    while let Some(item) = rn.pop() {
                        self.stored.push(item)
                    }
                }
                Err(e) => return Some(Err(e)),
            }
        }
        let next = self.stored.pop();
        if let Some(ref n) = next {
            if self.next_is_none.is_some() && if !n.nl_flags.contains(&NlmF::Multi) {
                self.next_is_none = Some(true);
            }
            if n.nl_type == NlTypeWrapper::Nlmsg(Nlmsg::Done) {
                return None;
            }
        }
        next.map(Ok)
    }
}

In particular, the line:

            if self.next_is_none.is_some() && if !n.nl_flags.contains(&NlmF::Multi) {

has two ifs.

Changes made by rustfmt

This is the diff I received when I ran rustfmt:

diff --git a/src/socket.rs b/src/socket.rs
index 029c27a..c3f562d 100644
--- a/src/socket.rs
+++ b/src/socket.rs
@@ -125,11 +125,7 @@ where
         }
         let next = self.stored.pop();
         if let Some(ref n) = next {
-            if self.next_is_none.is_some() && if !n.nl_flags.contains(&NlmF::Multi) {
-                self.next_is_none = Some(true);
             }
-            if n.nl_type == NlTypeWrapper::Nlmsg(Nlmsg::Done) {
-                return None;
             }
         }
         next.map(Ok)

Expected behavior

I would hope that rustfmt would throw a syntax error instead of deleting the code. Will rustfmt typically proceed even if there is a syntax error in the code? I don't think I've ever run rustfmt on malformed code before.

Metadata

Metadata

Labels

bugPanic, non-idempotency, invalid code, etc.p-high

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions