Skip to content

Commit a91ee28

Browse files
authored
tanzaku#29 fix clippy command in github actions and resolve clippy warning (tanzaku#30)
* fix: clippy warning * fix: clippy command * fix: doc test * fix: clippy
1 parent fa0c59c commit a91ee28

File tree

18 files changed

+169
-168
lines changed

18 files changed

+169
-168
lines changed

.github/workflows/rust.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ jobs:
2424
run: cargo bench
2525
- name: Run benchmarks with features
2626
run: cargo bench --features regex-match
27-
- name: clippy
28-
uses: actions-rs/clippy-check@v1
29-
with:
30-
token: ${{ secrets.GITHUB_TOKEN }}
27+
- name: Clippy (automata)
28+
run: cargo clippy -p automata -- -D warnings
29+
- name: Clippy (lexer-generator)
30+
run: cargo clippy -p lexer-generator -- -D warnings
31+
- name: Clippy (parser-generator)
32+
run: cargo clippy -p parser-generator -- -D warnings
33+
- name: Clippy (postgresql-cst-parser)
34+
run: cargo clippy -p postgresql-cst-parser -- -D warnings

crates/automata/src/dfa.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(clippy::mutable_key_type)]
12
use std::collections::{BTreeMap, BTreeSet};
23

34
use crate::nfa::{NFAState, Transition, collect_epsilon_closure};
@@ -21,7 +22,7 @@ pub struct DFA {
2122
pub states: Vec<DFAState>,
2223
}
2324

24-
impl<'a> DFA {
25+
impl DFA {
2526
pub fn match_bytes(&self, bs: &[u8]) -> Option<u32> {
2627
let mut state = 0;
2728

crates/automata/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn build_nfa_from_regex_node<'a>(
127127
RegexpNode::Reference(name) => {
128128
let regexp_node = named_pattern_definitions
129129
.get(name)
130-
.expect(&format!("Referenced pattern '{}' not found", name));
130+
.unwrap_or_else(|| panic!("Referenced pattern '{}' not found", name));
131131

132132
let node_nfa @ NFAFragment {
133133
start_state: node_start,
@@ -150,7 +150,7 @@ pub fn build_nfa_from_regex_node<'a>(
150150
accept_state: node_accept,
151151
} = NFAFragment::new(nfa);
152152

153-
build_nfa_from_regex_node(nfa, &node_nfa, &*node, named_pattern_definitions);
153+
build_nfa_from_regex_node(nfa, &node_nfa, node, named_pattern_definitions);
154154

155155
state.add_transition(node_start, Transition::Epsilon);
156156
state = node_accept;

crates/automata/src/nfa.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(clippy::mutable_key_type)]
12
use std::{
23
cell::{Cell, RefCell},
34
collections::{BTreeMap, BTreeSet},
@@ -18,6 +19,7 @@ pub enum Transition {
1819

1920
/// Represents a state in a Non-deterministic Finite Automaton.
2021
/// States are connected by transitions and can accept input.
22+
#[allow(clippy::mutable_key_type)]
2123
#[derive(Debug, Clone, Eq)]
2224
pub struct NFAState<'a> {
2325
pub state_id: usize,
@@ -37,7 +39,7 @@ impl PartialEq for NFAState<'_> {
3739

3840
impl PartialOrd for NFAState<'_> {
3941
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
40-
self.state_id.partial_cmp(&other.state_id)
42+
Some(self.state_id.cmp(&other.state_id))
4143
}
4244
}
4345

@@ -83,7 +85,7 @@ pub fn collect_epsilon_closure<'a>(
8385
let mut accepted_lexer_rule_id = None;
8486

8587
{
86-
let id = s.accept_lexer_rule_id.borrow().clone();
88+
let id = *s.accept_lexer_rule_id.borrow();
8789
if accepted_lexer_rule_id.unwrap_or(!0) > id.unwrap_or(!0) {
8890
accepted_lexer_rule_id = id;
8991
}
@@ -95,7 +97,7 @@ pub fn collect_epsilon_closure<'a>(
9597
if epsilon_closure.insert(new_state) {
9698
candidate.push(new_state);
9799

98-
let id = new_state.accept_lexer_rule_id.borrow().clone();
100+
let id = *new_state.accept_lexer_rule_id.borrow();
99101
if accepted_lexer_rule_id.unwrap_or(!0) > id.unwrap_or(!0) {
100102
accepted_lexer_rule_id = id;
101103
}
@@ -175,6 +177,12 @@ impl<'a> NFA<'a> {
175177
}
176178
}
177179

180+
impl Default for NFA<'_> {
181+
fn default() -> Self {
182+
NFA::new()
183+
}
184+
}
185+
178186
#[cfg(test)]
179187
mod tests {
180188
use super::*;

crates/automata/src/regexp.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ impl RegexpParser {
100100
}
101101

102102
let d = match c {
103-
b'0'..=b'9' => c as u8 - b'0',
104-
b'a'..=b'f' => c as u8 - b'a' + 10,
105-
b'A'..=b'F' => c as u8 - b'A' + 10,
103+
b'0'..=b'9' => c - b'0',
104+
b'a'..=b'f' => c - b'a' + 10,
105+
b'A'..=b'F' => c - b'A' + 10,
106106
_ => unreachable!(),
107107
};
108108

@@ -114,7 +114,7 @@ impl RegexpParser {
114114
}
115115
// If no format specified, treat as octal number
116116
b'0'..=b'9' => {
117-
let mut v = c as u8 - b'0';
117+
let mut v = c - b'0';
118118

119119
while self.index < self.bytes.len() {
120120
let c = self.bytes[self.index];
@@ -123,13 +123,13 @@ impl RegexpParser {
123123
break;
124124
}
125125

126-
v = v * 8 + (c as u8 - b'0');
126+
v = v * 8 + (c - b'0');
127127
self.index += 1;
128128
}
129129

130130
v
131131
}
132-
_ => c as u8,
132+
_ => c,
133133
}
134134
}
135135

@@ -163,7 +163,7 @@ impl RegexpParser {
163163
let bs = nodes
164164
.into_iter()
165165
.map(|node| match node {
166-
RegexpNode::Char(c) => c as u8,
166+
RegexpNode::Char(c) => c,
167167
_ => panic!(),
168168
})
169169
.collect::<Vec<_>>();
@@ -181,7 +181,7 @@ impl RegexpParser {
181181
_ => {
182182
self.index += 1;
183183
is_minus = c == b'-';
184-
nodes.push(RegexpNode::Char(c as u8))
184+
nodes.push(RegexpNode::Char(c))
185185
}
186186
}
187187

@@ -210,7 +210,7 @@ impl RegexpParser {
210210
break;
211211
}
212212

213-
nodes.push(RegexpNode::Char(c as u8));
213+
nodes.push(RegexpNode::Char(c));
214214
}
215215

216216
RegexpNode::concat(nodes)
@@ -310,7 +310,7 @@ impl RegexpParser {
310310
b']' | b')' | b'}' => unreachable!(),
311311
_ => {
312312
self.index += 1;
313-
RegexpNode::Char(c as u8)
313+
RegexpNode::Char(c)
314314
}
315315
}
316316
}
@@ -354,6 +354,12 @@ impl RegexpParser {
354354
}
355355
}
356356

357+
impl Default for RegexpParser {
358+
fn default() -> Self {
359+
RegexpParser::new()
360+
}
361+
}
362+
357363
#[cfg(test)]
358364
mod tests {
359365
use super::*;

crates/lexer-generator/src/flex_file.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ fn parse_pattern_actions(
5353
return None;
5454
}
5555

56-
if line.starts_with("<<EOF>>") {
57-
let rest = line["<<EOF>>".len()..].to_owned();
56+
if let Some(stripped) = line.strip_prefix("<<EOF>>") {
57+
let rest = stripped.to_owned();
5858
let actions = parse_actions(rest, it);
5959
return Some(("<<EOF>>".to_owned(), actions));
6060
}
@@ -110,7 +110,7 @@ fn parse_rules(states: Vec<String>, it: &mut impl Iterator<Item = String>) -> Ve
110110
}
111111

112112
fn skip_c_source(it: &mut impl Iterator<Item = String>) {
113-
while let Some(line) = it.next() {
113+
for line in it {
114114
if line == "%}" {
115115
break;
116116
}
@@ -136,8 +136,8 @@ pub fn parse_flex_file(s: impl AsRef<str>) -> FlexFile {
136136
continue;
137137
}
138138

139-
if line.starts_with("%x ") {
140-
flex_file.all_states.push(line[3..].to_owned());
139+
if let Some(rest) = line.strip_prefix("%x ") {
140+
flex_file.all_states.push(rest.to_owned());
141141
continue;
142142
}
143143

@@ -156,9 +156,9 @@ pub fn parse_flex_file(s: impl AsRef<str>) -> FlexFile {
156156

157157
flex_file.rules = parse_rules(Vec::new(), &mut it);
158158

159-
while let Some(line) = it.next() {
159+
for line in it {
160160
flex_file.rest_code += &line;
161-
flex_file.rest_code += &"\n";
161+
flex_file.rest_code += "\n";
162162
}
163163

164164
flex_file

crates/lexer-generator/src/lexer_generator.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,12 @@ fn construct_actions(flex_file: &FlexFile) -> String {
3333

3434
// If the action is |, the subsequent rule will be executed
3535
if rule.actions.trim() == "|" {
36-
res.push(format!(
37-
r#"RuleKind::{kind}|"#,
38-
kind = format!("{}{}", s, e)
39-
));
36+
res.push(format!(r#"RuleKind::{s}{e}|"#));
4037
} else {
4138
res.push(format!(
42-
r#"RuleKind::{kind} => {{
39+
r#"RuleKind::{s}{e} => {{
4340
{actions}
4441
}}"#,
45-
kind = format!("{}{}", s, e),
4642
actions = rule.actions
4743
));
4844
}
@@ -70,12 +66,13 @@ fn construct_pattern_actions_by_index(flex_file: &FlexFile) -> String {
7066
}
7167

7268
// どのルールにもマッチしなかったときの値
73-
res.push(format!(r#"255 => {{}}"#));
74-
res.push(format!(
69+
res.push(r#"255 => {{}}"#.to_string());
70+
res.push(
7571
r#"_ => {{
7672
unreachable!()
7773
}}"#
78-
));
74+
.to_string(),
75+
);
7976

8077
res.join("\n")
8178
}
@@ -129,7 +126,7 @@ fn extract_rule_pattern(flex_file: &FlexFile, pattern: &str) -> (String, bool) {
129126

130127
// Expand {xxx} to actual regular expression patterns
131128
let replaced = p
132-
.replace_all(&pattern, |caps: &regex::Captures| {
129+
.replace_all(pattern, |caps: &regex::Captures| {
133130
let name = caps.get(1).unwrap().as_str();
134131

135132
// Check if xxx in {xxx} is defined
@@ -163,13 +160,12 @@ fn construct_rule_defs(flex_file: &FlexFile) -> String {
163160
Rule {{
164161
state: State::{s},
165162
pattern: regex::bytes::Regex::new(r#"(?-u)^({pattern})"#).unwrap(),
166-
kind: RuleKind::{rule_kind},
163+
kind: RuleKind::{s}{e},
167164
eof: {eof},
168165
}}"###,
169166
original_pattern = rule.pattern,
170167
pattern = pattern,
171168
eof = eof,
172-
rule_kind = format!("{}{}", s, e),
173169
));
174170
}
175171
}

crates/parser-generator/src/parser_generator.rs

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn compress_action_table(
7272
fn find_most_common_action(
7373
uncompressed_action_table: &[i16],
7474
state_index: usize,
75-
terminal_symbols: &Vec<Component>,
75+
terminal_symbols: &[Component],
7676
) -> i16 {
7777
let mut cnt: BTreeMap<i16, u16> = BTreeMap::new();
7878
for j in 0..terminal_symbols.len() {
@@ -116,19 +116,14 @@ fn compress_action_table(
116116
ok = false;
117117
break;
118118
}
119-
} else {
120-
if index_check[i + j] == j as i16 {
121-
if compressed_values[i + j] != a {
122-
ok = false;
123-
break;
124-
}
125-
} else if index_check[i + j] != UNUSED_ENTRY_CODE {
126-
ok = false;
127-
break;
128-
} else if offset_used[i] {
119+
} else if index_check[i + j] == j as i16 {
120+
if compressed_values[i + j] != a {
129121
ok = false;
130122
break;
131123
}
124+
} else if index_check[i + j] != UNUSED_ENTRY_CODE || offset_used[i] {
125+
ok = false;
126+
break;
132127
}
133128
}
134129

@@ -154,7 +149,7 @@ fn compress_action_table(
154149
let max_table_index = *state_to_offset.iter().max().unwrap() as usize + terminal_symbols.len();
155150

156151
let info = CompressedParserTable {
157-
state_to_offset: state_to_offset,
152+
state_to_offset,
158153
compressed_values: compressed_values[..max_table_index].to_vec(),
159154
index_check: index_check[..max_table_index].to_vec(),
160155
};
@@ -199,19 +194,14 @@ fn compress_goto_table(
199194
ok = false;
200195
break;
201196
}
202-
} else {
203-
if index_check[i + j] == j as i16 {
204-
if compressed_values[i + j] != a {
205-
ok = false;
206-
break;
207-
}
208-
} else if index_check[i + j] != UNUSED_ENTRY_CODE {
209-
ok = false;
210-
break;
211-
} else if offset_used[i] {
197+
} else if index_check[i + j] == j as i16 {
198+
if compressed_values[i + j] != a {
212199
ok = false;
213200
break;
214201
}
202+
} else if index_check[i + j] != UNUSED_ENTRY_CODE || offset_used[i] {
203+
ok = false;
204+
break;
215205
}
216206
}
217207

@@ -236,13 +226,11 @@ fn compress_goto_table(
236226
let max_table_index =
237227
*state_to_offset.iter().max().unwrap() as usize + non_terminal_symbols.len();
238228

239-
let info = CompressedParserTable {
240-
state_to_offset: state_to_offset,
229+
CompressedParserTable {
230+
state_to_offset,
241231
compressed_values: compressed_values[..max_table_index].to_vec(),
242232
index_check: index_check[..max_table_index].to_vec(),
243-
};
244-
245-
info
233+
}
246234
}
247235

248236
fn generate_parser_source_code(
@@ -259,8 +247,8 @@ fn generate_parser_source_code(
259247
.join(",")
260248
}
261249

262-
let (action_table_info, def_rules) = compress_action_table(lalr, &terminal_symbols);
263-
let goto_table_info = compress_goto_table(lalr, &non_terminal_symbols);
250+
let (action_table_info, def_rules) = compress_action_table(lalr, terminal_symbols);
251+
let goto_table_info = compress_goto_table(lalr, non_terminal_symbols);
264252

265253
let terminal_symbols_with_comment = terminal_symbols
266254
.iter()
@@ -378,9 +366,9 @@ fn generate_parser_source_code(
378366
}
379367

380368
fn generate_syntax_kinds_source_code(
381-
terminal_symbols: &Vec<Component>,
382-
non_terminal_symbols: &Vec<Component>,
383-
comments: &Vec<Component>,
369+
terminal_symbols: &[Component],
370+
non_terminal_symbols: &[Component],
371+
comments: &[Component],
384372
) {
385373
let mut kinds = Vec::new();
386374
for c in terminal_symbols

0 commit comments

Comments
 (0)