Skip to content

Commit e2a1f38

Browse files
authored
Merge pull request #427 from ratmice/clippy_fallout
Clippy fallout & overzealous #[deny(unsafe_code)]
2 parents 4ed68d6 + bbc634c commit e2a1f38

6 files changed

Lines changed: 36 additions & 25 deletions

File tree

cfgrammar/src/lib/yacc/grammar.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -833,8 +833,7 @@ where
833833
// needed). If the first column spills, then we're done. This is basically normal
834834
// arithmetic but with each digit having an arbitrary base.
835835

836-
let mut todo = Vec::new();
837-
todo.resize(prod.len(), 0);
836+
let mut todo = vec![0; prod.len()];
838837
let mut cur = Vec::new();
839838
'b: loop {
840839
for i in 0..todo.len() {
@@ -893,10 +892,8 @@ where
893892
// means that we can iteratively improve our knowledge of a token's minimum cost:
894893
// eventually we will reach a point where we can determine it definitively.
895894

896-
let mut costs = vec![];
897-
costs.resize(usize::from(grm.rules_len()), 0);
898-
let mut done = vec![];
899-
done.resize(usize::from(grm.rules_len()), false);
895+
let mut costs = vec![0; usize::from(grm.rules_len())];
896+
let mut done = vec![false; usize::from(grm.rules_len())];
900897
loop {
901898
let mut all_done = true;
902899
for i in 0..done.len() {
@@ -960,10 +957,8 @@ fn rule_max_costs<StorageT: 'static + PrimInt + Unsigned>(
960957
where
961958
usize: AsPrimitive<StorageT>,
962959
{
963-
let mut done = vec![];
964-
done.resize(usize::from(grm.rules_len()), false);
965-
let mut costs = vec![];
966-
costs.resize(usize::from(grm.rules_len()), 0);
960+
let mut done = vec![false; usize::from(grm.rules_len())];
961+
let mut costs = vec![0; usize::from(grm.rules_len())];
967962

968963
// First mark all recursive rules.
969964
for ridx in grm.iter_rules() {

lrpar/cttests/src/calc_unsafeaction.test

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ grammar: |
55
%actiontype Result<u64, ()>
66
%avoid_insert 'INT'
77
%%
8-
Expr: Expr '+' Term { unsafe { Ok(Ok::<u64, ()>($1? + $3?).unwrap_unchecked()) } }
8+
Expr: Expr '+' Term { unsafe { unsafe_ok($1? + $3?) } }
99
| Term { $1 }
1010
;
1111

12-
Term: Term '*' Factor { unsafe { Ok(Ok::<u64, ()>($1? * $3?).unwrap_unchecked()) } }
12+
Term: Term '*' Factor { unsafe { unsafe_ok($1? * $3?) } }
1313
| Factor { $1 }
1414
;
1515

1616
Factor: '(' Expr ')' { $2 }
1717
| 'INT' {
1818
let l = $1.map_err(|_| ())?;
1919
match $lexer.span_str(l.span()).parse::<u64>() {
20-
Ok(v) => unsafe { Ok(Ok::<u64, ()>(v).unwrap_unchecked()) },
20+
Ok(v) => unsafe { unsafe_ok(v) },
2121
Err(_) => {
2222
let ((_, col), _) = $lexer.line_col(l.span());
2323
eprintln!("Error at column {}: '{}' cannot be represented as a u64",
@@ -28,6 +28,11 @@ grammar: |
2828
}
2929
}
3030
;
31+
%%
32+
// Just check that unsafe blocks work in actions.
33+
unsafe fn unsafe_ok<T, E>(x:T) -> Result<T, E> {
34+
Ok(x)
35+
}
3136

3237
lexer: |
3338
%%

lrpar/cttests/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ fn test_parseparam() {
242242
let lexerdef = parseparam_l::lexerdef();
243243
let lexer = lexerdef.lexer("101");
244244
match parseparam_y::parse(&lexer, &3) {
245-
(Some(i), _) if i == 104 => (),
245+
(Some(104), _) => (),
246246
_ => unreachable!(),
247247
}
248248
}

lrpar/cttests_macro/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn generate_codegen_fail_tests(item: TokenStream) -> TokenStream {
1717
let manifest_dir = std::path::Path::new(&manifest_dir)
1818
.strip_prefix(cwd)
1919
.unwrap();
20-
let test_glob_path = manifest_dir.join(&test_glob_str.value());
20+
let test_glob_path = manifest_dir.join(test_glob_str.value());
2121
let test_glob_str = test_glob_path.into_os_string().into_string().unwrap();
2222
let test_files = glob(&test_glob_str).unwrap();
2323
for file in test_files {

lrpar/src/lib/ctbuilder.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -676,12 +676,19 @@ where
676676
) -> Result<(), Box<dyn Error>> {
677677
let mut outs = String::new();
678678
writeln!(outs, "{} mod {} {{", self.visibility.cow_str(), mod_name).ok();
679+
// Emit user program section, and actions at the top so they may specify inner attributes.
680+
if let Some(YaccKind::Original(YaccOriginalActionKind::UserAction) | YaccKind::Grmtools) =
681+
self.yacckind
682+
{
683+
outs.push_str(&self.gen_user_actions(grm));
684+
}
685+
outs.push_str(" mod _parser_ {\n");
679686
outs.push_str(
680-
" #![allow(clippy::type_complexity)]
681-
#![allow(clippy::unnecessary_wraps)]
682-
#![deny(unsafe_code)]
683-
#[allow(unused_imports)]
684-
use ::lrpar::Lexeme;
687+
" #![allow(clippy::type_complexity)]
688+
#![allow(clippy::unnecessary_wraps)]
689+
#![deny(unsafe_code)]
690+
#[allow(unused_imports)]
691+
use super::*;
685692
",
686693
);
687694

@@ -691,13 +698,17 @@ where
691698
match self.yacckind.unwrap() {
692699
YaccKind::Original(YaccOriginalActionKind::UserAction) | YaccKind::Grmtools => {
693700
outs.push_str(&self.gen_wrappers(grm));
694-
outs.push_str(&self.gen_user_actions(grm));
695701
}
696702
YaccKind::Original(YaccOriginalActionKind::NoAction)
697703
| YaccKind::Original(YaccOriginalActionKind::GenericParseTree) => (),
698704
_ => unreachable!(),
699705
}
700-
outs.push_str("}\n\n");
706+
outs.push_str(" } // End of `mod _parser_`\n\n");
707+
outs.push_str(" #[allow(unused_imports)]\n");
708+
outs.push_str(" pub use _parser_::*;\n");
709+
outs.push_str(" #[allow(unused_imports)]\n");
710+
outs.push_str(" use ::lrpar::Lexeme;\n");
711+
outs.push_str("} // End of `mod {mod_name}` \n\n");
701712

702713
// Output the cache so that we can check whether the IDs map is stable.
703714
outs.push_str(cache);
@@ -1103,6 +1114,7 @@ where
11031114
if let Some(s) = grm.programs() {
11041115
outs.push_str("\n// User code from the program section\n\n");
11051116
outs.push_str(s);
1117+
outs.push_str("\n// End of user code from the program section\n\n");
11061118
}
11071119

11081120
// Convert actions to functions
@@ -1144,7 +1156,6 @@ where
11441156
outs,
11451157
" // {rulename}
11461158
#[allow(clippy::too_many_arguments)]
1147-
#[allow(unsafe_code)] // Allow an action to embed unsafe blocks within it.
11481159
fn {prefix}action_{}<'lexer, 'input: 'lexer>({prefix}ridx: ::cfgrammar::RIdx<{storaget}>,
11491160
{prefix}lexer: &'lexer dyn ::lrpar::NonStreamingLexer<'input, {lexertypest}>,
11501161
{prefix}span: ::cfgrammar::Span,

lrtable/src/lib/statetable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,8 @@ D : D;
988988
Ok(_) => panic!("Infinitely recursive rule let through"),
989989
Err(StateTableError {
990990
kind: StateTableErrorKind::AcceptReduceConflict(_),
991-
pidx,
992-
}) if pidx == PIdx(1) => (),
991+
pidx: PIdx(1),
992+
}) => (),
993993
Err(e) => panic!("Incorrect error returned {:?}", e),
994994
}
995995
}

0 commit comments

Comments
 (0)