From dafdf17f725abe44718e87915ae657822d9da838 Mon Sep 17 00:00:00 2001 From: Brummel Date: Sun, 10 May 2026 05:14:53 +0200 Subject: [PATCH] iter 22-tidy.7: extract duplicate_clause_err helper, retire 17 inline blocks --- crates/ailang-surface/src/parse.rs | 161 ++++++----------------------- 1 file changed, 31 insertions(+), 130 deletions(-) diff --git a/crates/ailang-surface/src/parse.rs b/crates/ailang-surface/src/parse.rs index ed68f69..fd884a7 100644 --- a/crates/ailang-surface/src/parse.rs +++ b/crates/ailang-surface/src/parse.rs @@ -374,14 +374,7 @@ impl<'a> Parser<'a> { match self.peek_head_ident() { Some("doc") => { if doc.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "data-def", - message: format!( - "data `{name}` has duplicate `(doc ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("data-def", &format!("data `{name}`"), "doc")); } doc = Some(self.parse_doc()?); } @@ -495,54 +488,26 @@ impl<'a> Parser<'a> { match self.peek_head_ident() { Some("doc") => { if doc.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "fn-def", - message: format!( - "fn `{name}` has duplicate `(doc ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("fn-def", &format!("fn `{name}`"), "doc")); } doc = Some(self.parse_doc()?); } Some("suppress") => suppress.push(self.parse_suppress_attr()?), Some("type") => { if ty.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "fn-def", - message: format!( - "fn `{name}` has duplicate `(type ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("fn-def", &format!("fn `{name}`"), "type")); } ty = Some(self.parse_type_attr()?); } Some("params") => { if params.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "fn-def", - message: format!( - "fn `{name}` has duplicate `(params ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("fn-def", &format!("fn `{name}`"), "params")); } params = Some(self.parse_params_attr()?); } Some("body") => { if body.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "fn-def", - message: format!( - "fn `{name}` has duplicate `(body ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("fn-def", &format!("fn `{name}`"), "body")); } body = Some(self.parse_body_attr()?); } @@ -657,6 +622,20 @@ impl<'a> Parser<'a> { } } + fn duplicate_clause_err( + &self, + production: &'static str, + subject: &str, + clause: &'static str, + ) -> ParseError { + let pos = self.peek().map(|t| t.span.start).unwrap_or(0); + ParseError::Production { + production, + message: format!("{subject} has duplicate `({clause} ...)` clause"), + pos, + } + } + fn parse_type_attr(&mut self) -> Result { self.expect_lparen("type-attr")?; self.expect_keyword("type")?; @@ -697,40 +676,19 @@ impl<'a> Parser<'a> { match self.peek_head_ident() { Some("doc") => { if doc.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "const-def", - message: format!( - "const `{name}` has duplicate `(doc ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("const-def", &format!("const `{name}`"), "doc")); } doc = Some(self.parse_doc()?); } Some("type") => { if ty.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "const-def", - message: format!( - "const `{name}` has duplicate `(type ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("const-def", &format!("const `{name}`"), "type")); } ty = Some(self.parse_type_attr()?); } Some("body") => { if value.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "const-def", - message: format!( - "const `{name}` has duplicate `(body ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("const-def", &format!("const `{name}`"), "body")); } value = Some(self.parse_body_attr()?); } @@ -780,14 +738,7 @@ impl<'a> Parser<'a> { match self.peek_head_ident() { Some("param") => { if param.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "class-def", - message: format!( - "class `{name}` has duplicate `(param ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("class-def", &format!("class `{name}`"), "param")); } self.expect_lparen("class.param")?; self.expect_keyword("param")?; @@ -796,27 +747,13 @@ impl<'a> Parser<'a> { } Some("superclass") => { if superclass.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "class-def", - message: format!( - "class `{name}` has duplicate `(superclass ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("class-def", &format!("class `{name}`"), "superclass")); } superclass = Some(self.parse_superclass()?); } Some("doc") => { if doc.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "class-def", - message: format!( - "class `{name}` has duplicate `(doc ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("class-def", &format!("class `{name}`"), "doc")); } doc = Some(self.parse_doc()?); } @@ -878,27 +815,13 @@ impl<'a> Parser<'a> { match self.peek_head_ident() { Some("type") => { if ty.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "class.method", - message: format!( - "method `{name}` has duplicate `(type ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("class.method", &format!("method `{name}`"), "type")); } ty = Some(self.parse_type_attr()?); } Some("default") => { if default.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "class.method", - message: format!( - "method `{name}` has duplicate `(default ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("class.method", &format!("method `{name}`"), "default")); } default = Some(self.parse_default_attr()?); } @@ -945,12 +868,7 @@ impl<'a> Parser<'a> { match self.peek_head_ident() { Some("class") => { if class.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "instance-def", - message: "instance has duplicate `(class ...)` clause".into(), - pos, - }); + return Err(self.duplicate_clause_err("instance-def", "instance", "class")); } self.expect_lparen("instance.class")?; self.expect_keyword("class")?; @@ -959,23 +877,13 @@ impl<'a> Parser<'a> { } Some("type") => { if type_.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "instance-def", - message: "instance has duplicate `(type ...)` clause".into(), - pos, - }); + return Err(self.duplicate_clause_err("instance-def", "instance", "type")); } type_ = Some(self.parse_type_attr()?); } Some("doc") => { if doc.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "instance-def", - message: "instance has duplicate `(doc ...)` clause".into(), - pos, - }); + return Err(self.duplicate_clause_err("instance-def", "instance", "doc")); } doc = Some(self.parse_doc()?); } @@ -1023,14 +931,7 @@ impl<'a> Parser<'a> { match self.peek_head_ident() { Some("body") => { if body.is_some() { - let pos = self.peek().map(|t| t.span.start).unwrap_or(0); - return Err(ParseError::Production { - production: "instance.method", - message: format!( - "instance method `{name}` has duplicate `(body ...)` clause" - ), - pos, - }); + return Err(self.duplicate_clause_err("instance.method", &format!("instance method `{name}`"), "body")); } body = Some(self.parse_body_attr()?); }