Skip to content

Commit 08e0a70

Browse files
Removed all unreachable patterns I could find
1 parent 243bece commit 08e0a70

File tree

6 files changed

+104
-47
lines changed

6 files changed

+104
-47
lines changed

src/ast/ddl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ impl fmt::Display for ColumnOption {
18871887
GeneratedAs::Always => "ALWAYS",
18881888
GeneratedAs::ByDefault => "BY DEFAULT",
18891889
// ExpStored goes with an expression, handled above
1890-
GeneratedAs::ExpStored => unreachable!(),
1890+
GeneratedAs::ExpStored => "",
18911891
};
18921892
write!(f, "GENERATED {when} AS IDENTITY")?;
18931893
if sequence_options.is_some() {

src/ast/helpers/stmt_data_loading.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ impl fmt::Display for StageParamsObject {
9999

100100
impl fmt::Display for StageLoadSelectItem {
101101
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
102-
if self.alias.is_some() {
103-
write!(f, "{}.", self.alias.as_ref().unwrap())?;
102+
if let Some(alias) = &self.alias {
103+
write!(f, "{alias}.")?;
104104
}
105105
write!(f, "${}", self.file_col_num)?;
106-
if self.element.is_some() {
107-
write!(f, ":{}", self.element.as_ref().unwrap())?;
106+
if let Some(element) = &self.element {
107+
write!(f, ":{}", element)?;
108108
}
109-
if self.item_as.is_some() {
110-
write!(f, " AS {}", self.item_as.as_ref().unwrap())?;
109+
if let Some(item_as) = &self.item_as {
110+
write!(f, " AS {}", item_as)?;
111111
}
112112
Ok(())
113113
}

src/ast/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9820,8 +9820,8 @@ impl fmt::Display for ShowCharset {
98209820
} else {
98219821
write!(f, " CHARACTER SET")?;
98229822
}
9823-
if self.filter.is_some() {
9824-
write!(f, " {}", self.filter.as_ref().unwrap())?;
9823+
if let Some(filter) = &self.filter {
9824+
write!(f, " {filter}")?;
98259825
}
98269826
Ok(())
98279827
}

src/keywords.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ macro_rules! define_keywords {
6767
pub const ALL_KEYWORDS: &[&str] = &[
6868
$($ident),*
6969
];
70+
71+
impl core::fmt::Display for Keyword {
72+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
73+
match self {
74+
Keyword::NoKeyword => write!(f, "NoKeyword"),
75+
$(Keyword::$ident => write!(f, "{}", $ident),)*
76+
}
77+
}
78+
}
7079
};
7180
}
7281

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
// Splitting complex nodes (expressions, statements, types) into separate types
154154
// would bloat the API and hide intent. Extra memory is a worthwhile tradeoff.
155155
#![allow(clippy::large_enum_variant)]
156+
#![forbid(clippy::unreachable)]
156157

157158
// Allow proc-macros to find this crate
158159
extern crate self as sqlparser;

src/parser/mod.rs

Lines changed: 85 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,11 @@ impl<'a> Parser<'a> {
11941194
let mut id_parts: Vec<Ident> = vec![match t {
11951195
Token::Word(w) => w.into_ident(next_token.span),
11961196
Token::SingleQuotedString(s) => Ident::with_quote('\'', s),
1197-
_ => unreachable!(), // We matched above
1197+
_ => {
1198+
return Err(ParserError::ParserError(
1199+
"Internal parser error: unexpected token type".to_string(),
1200+
))
1201+
}
11981202
}];
11991203

12001204
while self.consume_token(&Token::Period) {
@@ -1641,7 +1645,11 @@ impl<'a> Parser<'a> {
16411645
Token::PGSquareRoot => UnaryOperator::PGSquareRoot,
16421646
Token::PGCubeRoot => UnaryOperator::PGCubeRoot,
16431647
Token::AtSign => UnaryOperator::PGAbs,
1644-
_ => unreachable!(),
1648+
_ => {
1649+
return Err(ParserError::ParserError(
1650+
"Internal parser error: unexpected unary operator token".to_string(),
1651+
))
1652+
}
16451653
};
16461654
Ok(Expr::UnaryOp {
16471655
op,
@@ -1709,18 +1717,22 @@ impl<'a> Parser<'a> {
17091717
Ok(Expr::Value(self.parse_value()?))
17101718
}
17111719
Token::LParen => {
1712-
let expr = if let Some(expr) = self.try_parse_expr_sub_query()? {
1713-
expr
1714-
} else if let Some(lambda) = self.try_parse_lambda()? {
1715-
return Ok(lambda);
1716-
} else {
1717-
let exprs = self.parse_comma_separated(Parser::parse_expr)?;
1718-
match exprs.len() {
1719-
0 => unreachable!(), // parse_comma_separated ensures 1 or more
1720-
1 => Expr::Nested(Box::new(exprs.into_iter().next().unwrap())),
1721-
_ => Expr::Tuple(exprs),
1722-
}
1723-
};
1720+
let expr =
1721+
if let Some(expr) = self.try_parse_expr_sub_query()? {
1722+
expr
1723+
} else if let Some(lambda) = self.try_parse_lambda()? {
1724+
return Ok(lambda);
1725+
} else {
1726+
let exprs = self.parse_comma_separated(Parser::parse_expr)?;
1727+
match exprs.len() {
1728+
0 => return Err(ParserError::ParserError(
1729+
"Internal parser error: parse_comma_separated returned empty list"
1730+
.to_string(),
1731+
)),
1732+
1 => Expr::Nested(Box::new(exprs.into_iter().next().unwrap())),
1733+
_ => Expr::Tuple(exprs),
1734+
}
1735+
};
17241736
self.expect_token(&Token::RParen)?;
17251737
Ok(expr)
17261738
}
@@ -3591,7 +3603,9 @@ impl<'a> Parser<'a> {
35913603
right: Box::new(right),
35923604
is_some: keyword == Keyword::SOME,
35933605
},
3594-
_ => unreachable!(),
3606+
unexpected_keyword => return Err(ParserError::ParserError(
3607+
format!("Internal parser error: expected any of {{ALL, ANY, SOME}}, got {unexpected_keyword:?}"),
3608+
)),
35953609
})
35963610
} else {
35973611
Ok(Expr::BinaryOp {
@@ -5590,13 +5604,14 @@ impl<'a> Parser<'a> {
55905604
} else {
55915605
None
55925606
};
5593-
let option = self
5594-
.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT])
5595-
.map(|keyword| match keyword {
5596-
Keyword::CASCADE => ReferentialAction::Cascade,
5597-
Keyword::RESTRICT => ReferentialAction::Restrict,
5598-
_ => unreachable!(),
5599-
});
5607+
let option = match self.parse_one_of_keywords(&[Keyword::CASCADE, Keyword::RESTRICT]) {
5608+
Some(Keyword::CASCADE) => Some(ReferentialAction::Cascade),
5609+
Some(Keyword::RESTRICT) => Some(ReferentialAction::Restrict),
5610+
Some(unexpected_keyword) => return Err(ParserError::ParserError(
5611+
format!("Internal parser error: expected any of {{CASCADE, RESTRICT}}, got {unexpected_keyword:?}"),
5612+
)),
5613+
None => None,
5614+
};
56005615
Ok(Statement::DropTrigger(DropTrigger {
56015616
if_exists,
56025617
trigger_name,
@@ -5646,7 +5661,9 @@ impl<'a> Parser<'a> {
56465661
match self.expect_one_of_keywords(&[Keyword::ROW, Keyword::STATEMENT])? {
56475662
Keyword::ROW => TriggerObject::Row,
56485663
Keyword::STATEMENT => TriggerObject::Statement,
5649-
_ => unreachable!(),
5664+
unexpected_keyword => return Err(ParserError::ParserError(
5665+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in ROW/STATEMENT"),
5666+
)),
56505667
};
56515668

56525669
Some(if include_each {
@@ -5709,7 +5726,9 @@ impl<'a> Parser<'a> {
57095726
Keyword::INSTEAD => self
57105727
.expect_keyword_is(Keyword::OF)
57115728
.map(|_| TriggerPeriod::InsteadOf)?,
5712-
_ => unreachable!(),
5729+
unexpected_keyword => return Err(ParserError::ParserError(
5730+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in trigger period"),
5731+
)),
57135732
},
57145733
)
57155734
}
@@ -5733,7 +5752,9 @@ impl<'a> Parser<'a> {
57335752
}
57345753
Keyword::DELETE => TriggerEvent::Delete,
57355754
Keyword::TRUNCATE => TriggerEvent::Truncate,
5736-
_ => unreachable!(),
5755+
unexpected_keyword => return Err(ParserError::ParserError(
5756+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in trigger event"),
5757+
)),
57375758
},
57385759
)
57395760
}
@@ -5767,7 +5788,9 @@ impl<'a> Parser<'a> {
57675788
{
57685789
Keyword::FUNCTION => TriggerExecBodyType::Function,
57695790
Keyword::PROCEDURE => TriggerExecBodyType::Procedure,
5770-
_ => unreachable!(),
5791+
unexpected_keyword => return Err(ParserError::ParserError(
5792+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in trigger exec body"),
5793+
)),
57715794
},
57725795
func_desc: self.parse_function_desc()?,
57735796
})
@@ -6284,7 +6307,9 @@ impl<'a> Parser<'a> {
62846307
Some(Keyword::CURRENT_USER) => Owner::CurrentUser,
62856308
Some(Keyword::CURRENT_ROLE) => Owner::CurrentRole,
62866309
Some(Keyword::SESSION_USER) => Owner::SessionUser,
6287-
Some(_) => unreachable!(),
6310+
Some(unexpected_keyword) => return Err(ParserError::ParserError(
6311+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in owner"),
6312+
)),
62886313
None => {
62896314
match self.parse_identifier() {
62906315
Ok(ident) => Owner::Ident(ident),
@@ -6346,7 +6371,9 @@ impl<'a> Parser<'a> {
63466371
Some(match keyword {
63476372
Keyword::PERMISSIVE => CreatePolicyType::Permissive,
63486373
Keyword::RESTRICTIVE => CreatePolicyType::Restrictive,
6349-
_ => unreachable!(),
6374+
unexpected_keyword => return Err(ParserError::ParserError(
6375+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in policy type"),
6376+
)),
63506377
})
63516378
} else {
63526379
None
@@ -6366,7 +6393,9 @@ impl<'a> Parser<'a> {
63666393
Keyword::INSERT => CreatePolicyCommand::Insert,
63676394
Keyword::UPDATE => CreatePolicyCommand::Update,
63686395
Keyword::DELETE => CreatePolicyCommand::Delete,
6369-
_ => unreachable!(),
6396+
unexpected_keyword => return Err(ParserError::ParserError(
6397+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in policy command"),
6398+
)),
63706399
})
63716400
} else {
63726401
None
@@ -7003,7 +7032,9 @@ impl<'a> Parser<'a> {
70037032
match keyword {
70047033
Keyword::WITH => Some(true),
70057034
Keyword::WITHOUT => Some(false),
7006-
_ => unreachable!(),
7035+
unexpected_keyword => return Err(ParserError::ParserError(
7036+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in cursor hold"),
7037+
)),
70077038
}
70087039
}
70097040
None => None,
@@ -9770,7 +9801,9 @@ impl<'a> Parser<'a> {
97709801
Keyword::PART => Ok(Partition::Part(self.parse_expr()?)),
97719802
Keyword::PARTITION => Ok(Partition::Expr(self.parse_expr()?)),
97729803
// unreachable because expect_one_of_keywords used above
9773-
_ => unreachable!(),
9804+
unexpected_keyword => Err(ParserError::ParserError(
9805+
format!("Internal parser error: expected any of {{PART, PARTITION}}, got {unexpected_keyword:?}"),
9806+
)),
97749807
}
97759808
}
97769809

@@ -9825,7 +9858,9 @@ impl<'a> Parser<'a> {
98259858
Keyword::CONNECTOR => self.parse_alter_connector(),
98269859
Keyword::USER => self.parse_alter_user(),
98279860
// unreachable because expect_one_of_keywords used above
9828-
_ => unreachable!(),
9861+
unexpected_keyword => Err(ParserError::ParserError(
9862+
format!("Internal parser error: expected any of {{VIEW, TYPE, TABLE, INDEX, ROLE, POLICY, CONNECTOR, ICEBERG, SCHEMA, USER, OPERATOR}}, got {unexpected_keyword:?}"),
9863+
)),
98299864
}
98309865
}
98319866

@@ -10022,7 +10057,9 @@ impl<'a> Parser<'a> {
1002210057
Keyword::MERGES => {
1002310058
options.push(OperatorOption::Merges);
1002410059
}
10025-
_ => unreachable!(),
10060+
unexpected_keyword => return Err(ParserError::ParserError(
10061+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in operator option"),
10062+
)),
1002610063
}
1002710064

1002810065
if !self.consume_token(&Token::Comma) {
@@ -14291,7 +14328,9 @@ impl<'a> Parser<'a> {
1429114328
table = match kw {
1429214329
Keyword::PIVOT => self.parse_pivot_table_factor(table)?,
1429314330
Keyword::UNPIVOT => self.parse_unpivot_table_factor(table)?,
14294-
_ => unreachable!(),
14331+
unexpected_keyword => return Err(ParserError::ParserError(
14332+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in pivot/unpivot"),
14333+
)),
1429514334
}
1429614335
}
1429714336
return Ok(table);
@@ -14549,7 +14588,9 @@ impl<'a> Parser<'a> {
1454914588
table = match kw {
1455014589
Keyword::PIVOT => self.parse_pivot_table_factor(table)?,
1455114590
Keyword::UNPIVOT => self.parse_unpivot_table_factor(table)?,
14552-
_ => unreachable!(),
14591+
unexpected_keyword => return Err(ParserError::ParserError(
14592+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in pivot/unpivot"),
14593+
)),
1455314594
}
1455414595
}
1455514596

@@ -15648,7 +15689,9 @@ impl<'a> Parser<'a> {
1564815689
}
1564915690
}
1565015691
Some(Keyword::TABLE) | None => Some(GrantObjects::Tables(objects?)),
15651-
_ => unreachable!(),
15692+
Some(unexpected_keyword) => return Err(ParserError::ParserError(
15693+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in grant objects"),
15694+
)),
1565215695
}
1565315696
}
1565415697
} else {
@@ -16518,7 +16561,9 @@ impl<'a> Parser<'a> {
1651816561
let kind = match self.expect_one_of_keywords(&[Keyword::MIN, Keyword::MAX])? {
1651916562
Keyword::MIN => HavingBoundKind::Min,
1652016563
Keyword::MAX => HavingBoundKind::Max,
16521-
_ => unreachable!(),
16564+
unexpected_keyword => return Err(ParserError::ParserError(
16565+
format!("Internal parser error: unexpected keyword `{unexpected_keyword}` in having bound"),
16566+
)),
1652216567
};
1652316568
clauses.push(FunctionArgumentClause::Having(HavingBound(
1652416569
kind,
@@ -17046,7 +17091,9 @@ impl<'a> Parser<'a> {
1704617091
let lock_type = match self.expect_one_of_keywords(&[Keyword::UPDATE, Keyword::SHARE])? {
1704717092
Keyword::UPDATE => LockType::Update,
1704817093
Keyword::SHARE => LockType::Share,
17049-
_ => unreachable!(),
17094+
unexpected_keyword => return Err(ParserError::ParserError(
17095+
format!("Internal parser error: expected any of {{UPDATE, SHARE}}, got {unexpected_keyword:?}"),
17096+
)),
1705017097
};
1705117098
let of = if self.parse_keyword(Keyword::OF) {
1705217099
Some(self.parse_object_name(false)?)

0 commit comments

Comments
 (0)