[rust][ci] Run cargo check/fmt
Formats the codebase, fixes some clippy lints, and updates CI to check that code is formatted.
Joe Savona committed
Aug 17, 2023 at 12:59 UTC
d5a8e801571fbada273171e646d6b50d4cd81992
19 files changed
+103
-73
compiler/.github/workflows/rust.yml
+43
-5
@@ -17,15 +17,53 @@ on:
17
18
env:
19
CARGO_TERM_COLOR: always
20
+ RUSTFLAGS: -Dwarnings
21
22
jobs:
23
test:
23
- runs-on: ubuntu-latest
24
+ name: Rust Test (${{ matrix.target.os }})
25
+ strategy:
26
+ matrix:
27
+ target:
28
+ - target: ubuntu-latest
29
+ os: ubuntu-latest
30
+ # TODO: run on more platforms
31
+ # - target: macos-latest
32
+ # os: macos-latest
33
+ # - target: windows-latest
34
+ # os: windows-latest
35
+ runs-on: ${{ matrix.target.os }}
36
steps:
37
- uses: actions/checkout@v3
26
- - name: Build
27
- run: cargo build --verbose
38
+ - name: cargo test
39
working-directory: forget
29
- - name: Run tests
30
- run: cargo test --verbose
40
+ run: cargo test --manifest-path=Cargo.toml --locked ${{ matrix.target.features && '--features' }} ${{ matrix.target.features }}
41
+
42
+ lint:
43
+ name: Rust Lint
44
+ runs-on: ubuntu-latest
45
+ steps:
46
+ - uses: actions/checkout@v2
47
+ - uses: actions-rs/toolchain@v1
48
+ with:
49
+ # NOTE: use `rustup run <toolchain> <command>` in commands below
50
+ # with this exact same toolchain value
51
+ toolchain: nightly-2023-08-01
52
+ override: true
53
+ components: rustfmt, clippy
54
+ - name: rustfmt
55
+ run: grep -r --include "*.rs" --files-without-match "@generated" crates | xargs rustup run nightly-2023-08-01 rustfmt --check --config="skip_children=true"
56
+ working-directory: ./forget/
57
+ # - name: cargo clippy
58
+ # working-directory: forget
59
+ # run: rustup run nightly-2023-08-01 cargo clippy -- -Dclippy::correctness
60
+
61
+ build:
62
+ name: Rust Build
63
+ runs-on: ubuntu-latest
64
+ # TODO: build on more platforms, deploy, etc
65
+ steps:
66
+ - uses: actions/checkout@v3
67
+ - name: cargo build
68
working-directory: forget
69
+ run: cargo build --release
compiler/forget/crates/forget_build_hir/src/build.rs
+4
-4
@@ -374,13 +374,13 @@ fn lower_expression(
374
Expression::AssignmentExpression(expr) => match expr.operator {
375
forget_estree::AssignmentOperator::Equals => {
376
let right = lower_expression(env, builder, &expr.right)?;
377
- return Ok(lower_assignment(
377
+ return lower_assignment(
378
env,
379
builder,
380
InstructionKind::Reassign,
381
&expr.left,
382
right,
383
- )?);
383
+ );
384
}
385
_ => todo!("lower assignment expr {:#?}", expr),
386
},
@@ -418,7 +418,7 @@ fn lower_expression(
418
return Err(Diagnostic::todo("Support method calls", expr.range));
419
}
420
421
- let callee = lower_expression(env, builder, &callee_expr)?;
421
+ let callee = lower_expression(env, builder, callee_expr)?;
422
let arguments = lower_arguments(env, builder, &expr.arguments)?;
423
InstructionValue::Call(forget_hir::Call { callee, arguments })
424
}
@@ -568,7 +568,7 @@ fn lower_assignment_pattern(
568
None => items.push(ArrayDestructureItem::Hole),
569
Some(Pattern::Identifier(element)) => {
570
let identifier =
571
- lower_identifier_for_assignment(env, builder, kind, &element)?;
571
+ lower_identifier_for_assignment(env, builder, kind, element)?;
572
items.push(ArrayDestructureItem::Value(identifier));
573
}
574
Some(Pattern::RestElement(element)) => {
compiler/forget/crates/forget_diagnostics/src/diagnostic.rs
+4
-4
@@ -2,7 +2,7 @@ use std::error::Error;
2
use std::fmt::{Debug, Display, Write};
3
4
use forget_estree::SourceRange;
5
-use miette::{ByteOffset, SourceSpan};
5
+use miette::SourceSpan;
6
use static_assertions::assert_impl_all;
7
use thiserror::Error;
8
@@ -189,7 +189,7 @@ impl Diagnostic {
189
190
impl Display for Diagnostic {
191
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
192
- write!(f, "{}", self.0.message.to_string())
192
+ write!(f, "{}", self.0.message)
193
}
194
}
195
@@ -272,7 +272,7 @@ impl From<Diagnostic> for Diagnostics {
272
273
fn source_span_from_range(range: SourceRange) -> SourceSpan {
274
SourceSpan::new(
275
- ByteOffset::from(range.start as usize).into(),
276
- ByteOffset::from((u32::from(range.end) - range.start) as usize).into(),
275
+ (range.start as usize).into(),
276
+ ((u32::from(range.end) - range.start) as usize).into(),
277
)
278
}
compiler/forget/crates/forget_estree/src/generated.rs
+1
@@ -3,6 +3,7 @@
3
#![allow(dead_code)]
4
#![allow(unused_variables)]
5
#![allow(non_snake_case)]
6
+#![allow(clippy::enum_variant_names)]
7
use std::num::NonZeroU32;
8
use serde::ser::{Serializer, SerializeMap};
9
use serde::{Serialize, Deserialize};
compiler/forget/crates/forget_estree/src/js_value.rs
+3
-7
@@ -15,7 +15,7 @@ impl JsValue {
15
match &self {
16
JsValue::Boolean(value) => *value,
17
JsValue::Number(value) => value.is_truthy(),
18
- JsValue::String(value) => value.len() != 0,
18
+ JsValue::String(value) => !value.is_empty(),
19
JsValue::Null => false,
20
JsValue::Undefined => false,
21
}
@@ -102,7 +102,7 @@ impl<'de> Deserialize<'de> for JsValue {
102
103
#[inline]
104
fn visit_i64<E>(self, value: i64) -> Result<JsValue, E> {
105
- if value >= MIN_SAFE_INT && value <= MAX_SAFE_INT {
105
+ if (MIN_SAFE_INT..=MAX_SAFE_INT).contains(&value) {
106
Ok(JsValue::Number((value as f64).into()))
107
} else {
108
panic!("Invalid number")
@@ -212,11 +212,7 @@ impl Number {
212
213
pub fn is_truthy(self) -> bool {
214
let value = f64::from(self);
215
- if self.0 == f64::NAN.to_bits() || value == 0.0 || value == -0.0 {
216
- false
217
- } else {
218
- true
219
- }
215
+ !(self.0 == f64::NAN.to_bits() || value == 0.0 || value == -0.0)
216
}
217
}
218
compiler/forget/crates/forget_estree/src/lib.rs
-1
@@ -15,7 +15,6 @@ pub use visit::*;
15
#[cfg(test)]
16
mod tests {
17
use insta::{assert_snapshot, glob};
18
- use serde_json;
18
19
use super::*;
20
compiler/forget/crates/forget_estree/src/visit.rs
+2
-2
@@ -14,14 +14,14 @@ use crate::{
14
pub trait Visitor_DEPRECATED<'ast> {
15
fn visit_lvalue<F>(&mut self, f: F)
16
where
17
- F: FnOnce(&mut Self) -> (),
17
+ F: FnOnce(&mut Self),
18
{
19
f(self);
20
}
21
22
fn visit_rvalue<F>(&mut self, f: F)
23
where
24
- F: FnOnce(&mut Self) -> (),
24
+ F: FnOnce(&mut Self),
25
{
26
f(self);
27
}
compiler/forget/crates/forget_estree_codegen/src/codegen.rs
+4
-3
@@ -92,6 +92,7 @@ impl Grammar {
92
#![allow(dead_code)]
93
#![allow(unused_variables)]
94
#![allow(non_snake_case)]
95
+ #![allow(clippy::enum_variant_names)]
96
97
use std::num::NonZeroU32;
98
use serde::ser::{Serializer, SerializeMap};
@@ -137,9 +138,9 @@ impl Grammar {
138
quote! {
139
#![allow(dead_code)]
140
#![allow(unused_variables)]
141
+ #![allow(clippy::enum_variant_names)]
142
143
use forget_estree::*;
142
- use hermes;
144
use hermes::parser::{NodePtr, NodeKind, NodeLabel };
145
use hermes::utf::{utf8_with_surrogates_to_string};
146
use crate::generated_extension::*;
@@ -335,7 +336,7 @@ impl Node {
336
.filter_map(|(name, field)| {
337
let (type_name_str, type_kind) = parse_type(&field.type_).unwrap();
338
if (!grammar.objects.contains_key(&type_name_str)
338
- || grammar.objects.get(&type_name_str).unwrap().visitor == false)
339
+ || !grammar.objects.get(&type_name_str).unwrap().visitor)
340
&& !grammar.nodes.contains_key(&type_name_str)
341
&& !grammar.enums.contains_key(&type_name_str)
342
{
@@ -926,7 +927,7 @@ fn parse_type(type_: &str) -> Result<(String, TypeKind), String> {
927
current = ¤t[7..current.len() - 1];
928
is_option = true;
929
}
929
- if current.contains("<") {
930
+ if current.contains('<') {
931
Err(format!(
932
"Unsupported type `{current}` expected named type (`Identifier`), optional type (`Option<Identifier>`), list type (`Vec<Identifier>`), or optional list (`Vec<Option<Identifier>>`)"
933
))
compiler/forget/crates/forget_hermes_parser/src/generated.rs
+1
-1
@@ -2,8 +2,8 @@
2
#![cfg_attr(rustfmt, rustfmt_skip)]
3
#![allow(dead_code)]
4
#![allow(unused_variables)]
5
+#![allow(clippy::enum_variant_names)]
6
use forget_estree::*;
6
-use hermes;
7
use hermes::parser::{NodePtr, NodeKind, NodeLabel};
8
use hermes::utf::utf8_with_surrogates_to_string;
9
use crate::generated_extension::*;
compiler/forget/crates/forget_hermes_parser/src/generated_extension.rs
+7
-7
@@ -19,8 +19,8 @@ use hermes::parser::{
19
hermes_get_FunctionExpression_generator, hermes_get_FunctionExpression_id,
20
hermes_get_FunctionExpression_params, hermes_get_Property_computed, hermes_get_Property_key,
21
hermes_get_Property_kind, hermes_get_Property_method, hermes_get_Property_shorthand,
22
- hermes_get_Property_value, NodeKind, NodeLabel, NodeLabelOpt, NodeListRef,
23
- NodePtr, NodePtrOpt, NodeString, NodeStringOpt, SMRange,
22
+ hermes_get_Property_value, NodeKind, NodeLabel, NodeLabelOpt, NodeListRef, NodePtr, NodePtrOpt,
23
+ NodeString, NodeStringOpt, SMRange,
24
};
25
use hermes::utf::utf8_with_surrogates_to_string;
26
use juno_support::NullTerminatedBuf;
@@ -31,7 +31,7 @@ pub struct Context {
31
32
impl Context {
33
pub fn new(parser: &NullTerminatedBuf) -> Self {
34
- let start: usize = unsafe { std::mem::transmute(parser.as_ptr()) };
34
+ let start: usize = unsafe { parser.as_ptr() as usize };
35
Self { start }
36
}
37
}
@@ -43,11 +43,11 @@ pub trait FromHermesLabel {
43
fn convert(cx: &mut Context, label: NodeLabel) -> Self;
44
}
45
46
-pub fn convert_option<F, T>(node: NodePtrOpt, mut f: F) -> Option<T>
46
+pub fn convert_option<F, T>(node: NodePtrOpt, f: F) -> Option<T>
47
where
48
F: FnMut(NodePtr) -> T,
49
{
50
- node.as_node_ptr().map(|node| f(node))
50
+ node.as_node_ptr().map(f)
51
}
52
53
pub fn convert_vec<F, T>(node: NodeListRef, mut f: F) -> Vec<T>
@@ -75,9 +75,9 @@ where
75
76
pub fn convert_range(cx: &Context, node: NodePtr) -> SourceRange {
77
let range = node.as_ref().source_range;
78
- let absolute_start: usize = unsafe { std::mem::transmute(range.start.as_ptr()) };
78
+ let absolute_start: usize = range.start.as_ptr() as usize;
79
let start = absolute_start - cx.start;
80
- let absolute_end: usize = unsafe { std::mem::transmute(range.end.as_ptr()) };
80
+ let absolute_end: usize = range.end.as_ptr() as usize;
81
let end = absolute_end - cx.start;
82
SourceRange {
83
start: start as u32,
compiler/forget/crates/forget_hermes_parser/src/lib.rs
+1
-1
@@ -23,7 +23,7 @@ pub fn parse(source: &str, _file: &str) -> Result<Program, Vec<Diagnostic>> {
23
if result.has_errors() {
24
let error_messages = result.messages();
25
return Err(error_messages
26
- .into_iter()
26
+ .iter()
27
.map(|diag| {
28
let message = utf8_with_surrogates_to_string(diag.message.as_slice()).unwrap();
29
Diagnostic::invalid_syntax(message, None)
compiler/forget/crates/forget_hermes_parser/tests/parser_test.rs
-1
@@ -3,7 +3,6 @@ use std::env;
3
use forget_estree::SourceType;
4
use forget_hermes_parser::parse;
5
use insta::{assert_snapshot, glob};
6
-use serde_json;
6
7
#[test]
8
fn fixtures() {
compiler/forget/crates/forget_hir/src/function.rs
+2
-2
@@ -32,7 +32,7 @@ pub struct HIR {
32
}
33
34
impl HIR {
35
- pub fn inline(&mut self, other: FunctionExpression) -> () {
35
+ pub fn inline(&mut self, other: FunctionExpression) {
36
let offset = self.instructions.len();
37
for instr in other.lowered_function.body.instructions.into_iter() {
38
self.instructions.push(instr);
@@ -197,7 +197,7 @@ impl<'blocks> BlockRewriter<'blocks> {
197
}
198
}
199
200
- pub fn each_block<F>(&mut self, mut f: F) -> ()
200
+ pub fn each_block<F>(&mut self, mut f: F)
201
where
202
F: FnMut(Box<BasicBlock>, &mut Self) -> BlockRewriterAction,
203
{
compiler/forget/crates/forget_hir/src/instruction.rs
+6
-6
@@ -14,9 +14,9 @@ pub struct Instruction {
14
}
15
16
impl Instruction {
17
- pub fn each_lvalue<F>(&mut self, mut f: F) -> ()
17
+ pub fn each_lvalue<F>(&mut self, mut f: F)
18
where
19
- F: FnMut(&mut IdentifierOperand) -> (),
19
+ F: FnMut(&mut IdentifierOperand),
20
{
21
match &mut self.value {
22
InstructionValue::DeclareContext(instr) => {
@@ -75,9 +75,9 @@ impl Instruction {
75
Ok(())
76
}
77
78
- pub fn each_rvalue<F>(&mut self, mut f: F) -> ()
78
+ pub fn each_rvalue<F>(&mut self, mut f: F)
79
where
80
- F: FnMut(&mut IdentifierOperand) -> (),
80
+ F: FnMut(&mut IdentifierOperand),
81
{
82
match &mut self.value {
83
InstructionValue::Array(value) => {
@@ -303,9 +303,9 @@ impl DestructurePattern {
303
}
304
Ok(())
305
}
306
- pub fn each_operand<F>(&mut self, f: &mut F) -> ()
306
+ pub fn each_operand<F>(&mut self, f: &mut F)
307
where
308
- F: FnMut(&mut IdentifierOperand) -> (),
308
+ F: FnMut(&mut IdentifierOperand),
309
{
310
match self {
311
Self::Array(elements) => {
compiler/forget/crates/forget_hir/src/terminal.rs
+3
-3
@@ -30,7 +30,7 @@ pub enum TerminalValue {
30
}
31
32
impl TerminalValue {
33
- pub fn map_optional_fallthroughs<F>(&mut self, f: F) -> ()
33
+ pub fn map_optional_fallthroughs<F>(&mut self, f: F)
34
where
35
F: Fn(BlockId) -> Option<BlockId>,
36
{
@@ -85,9 +85,9 @@ impl TerminalValue {
85
}
86
}
87
88
- pub fn each_operand<F>(&mut self, mut f: F) -> ()
88
+ pub fn each_operand<F>(&mut self, mut f: F)
89
where
90
- F: FnMut(&mut IdentifierOperand) -> (),
90
+ F: FnMut(&mut IdentifierOperand),
91
{
92
match self {
93
TerminalValue::Branch(terminal) => f(&mut terminal.test),
compiler/forget/crates/forget_optimization/src/constant_propagation.rs
+4
-8
@@ -220,15 +220,11 @@ fn apply_binary_operator(
220
_ => None,
221
},
222
(left, right) => match operator {
223
- BinaryOperator::Equals => left
224
- .loosely_equals(&right)
225
- .map(|value| JsValue::Boolean(value)),
226
- BinaryOperator::NotEquals => left
227
- .not_loosely_equals(&right)
228
- .map(|value| JsValue::Boolean(value)),
229
- BinaryOperator::StrictEquals => Some(JsValue::Boolean(left.strictly_equals(&right))),
223
+ BinaryOperator::Equals => left.loosely_equals(right).map(JsValue::Boolean),
224
+ BinaryOperator::NotEquals => left.not_loosely_equals(right).map(JsValue::Boolean),
225
+ BinaryOperator::StrictEquals => Some(JsValue::Boolean(left.strictly_equals(right))),
226
BinaryOperator::NotStrictEquals => {
231
- Some(JsValue::Boolean(left.not_strictly_equals(&right)))
227
+ Some(JsValue::Boolean(left.not_strictly_equals(right)))
228
}
229
_ => None,
230
},
compiler/forget/crates/forget_semantic_analysis/src/analyzer.rs
+2
-2
@@ -79,7 +79,7 @@ impl Analyzer {
79
80
fn enter_label<F>(&mut self, id: LabelId, mut f: F)
81
where
82
- F: FnMut(&mut Self) -> (),
82
+ F: FnMut(&mut Self),
83
{
84
self.labels.push(id);
85
f(self);
@@ -127,7 +127,7 @@ impl Analyzer {
127
128
fn enter<F>(&mut self, kind: ScopeKind, mut f: F) -> ScopeId
129
where
130
- F: FnMut(&mut Self) -> (),
130
+ F: FnMut(&mut Self),
131
{
132
let scope = self.enter_scope(kind);
133
f(self);
compiler/forget/crates/forget_semantic_analysis/src/scope_view.rs
+11
-11
@@ -13,7 +13,7 @@ pub struct ScopeManagerView<'m> {
13
impl<'m> ScopeManagerView<'m> {
14
pub fn root(&self) -> ScopeView<'m> {
15
ScopeView {
16
- manager: &self.manager,
16
+ manager: self.manager,
17
scope: self.manager.scope(self.manager.root_id()),
18
}
19
}
@@ -50,7 +50,7 @@ impl<'m> ScopeView<'m> {
50
self.scope.parent.map(|id| {
51
let scope = self.manager.scope(id);
52
ScopeView {
53
- manager: &self.manager,
53
+ manager: self.manager,
54
scope,
55
}
56
})
@@ -64,7 +64,7 @@ impl<'m> ScopeView<'m> {
64
.map(|id| {
65
let declaration = self.manager.declaration(id);
66
DeclarationView {
67
- manager: &self.manager,
67
+ manager: self.manager,
68
declaration,
69
}
70
})
@@ -79,7 +79,7 @@ impl<'m> ScopeView<'m> {
79
.map(|id| {
80
let reference = self.manager.reference(id);
81
ReferenceView {
82
- manager: &self.manager,
82
+ manager: self.manager,
83
reference,
84
}
85
})
@@ -94,7 +94,7 @@ impl<'m> ScopeView<'m> {
94
.map(|id| {
95
let scope = self.manager.scope(id);
96
ScopeView {
97
- manager: &self.manager,
97
+ manager: self.manager,
98
scope,
99
}
100
})
@@ -117,7 +117,7 @@ impl<'m> std::fmt::Debug for ScopeView<'m> {
117
(
118
name.clone(),
119
DeclarationView {
120
- manager: &self.manager,
120
+ manager: self.manager,
121
declaration: self.manager.declaration(*declaration),
122
},
123
)
@@ -128,7 +128,7 @@ impl<'m> std::fmt::Debug for ScopeView<'m> {
128
.references
129
.iter()
130
.map(|reference| ReferenceView {
131
- manager: &self.manager,
131
+ manager: self.manager,
132
reference: self.manager.reference(*reference),
133
})
134
.collect();
@@ -137,7 +137,7 @@ impl<'m> std::fmt::Debug for ScopeView<'m> {
137
.children
138
.iter()
139
.map(|child| ScopeView {
140
- manager: &self.manager,
140
+ manager: self.manager,
141
scope: self.manager.scope(*child),
142
})
143
.collect();
@@ -190,7 +190,7 @@ impl<'m> DeclarationView<'m> {
190
pub fn scope(&self) -> ScopeView<'m> {
191
let scope = self.manager.scope(self.declaration.scope);
192
ScopeView {
193
- manager: &self.manager,
193
+ manager: self.manager,
194
scope,
195
}
196
}
@@ -224,7 +224,7 @@ impl<'m> ReferenceView<'m> {
224
pub fn scope(&self) -> ScopeView<'m> {
225
let scope = self.manager.scope(self.reference.scope);
226
ScopeView {
227
- manager: &self.manager,
227
+ manager: self.manager,
228
scope,
229
}
230
}
@@ -232,7 +232,7 @@ impl<'m> ReferenceView<'m> {
232
pub fn declaration(&self) -> DeclarationView<'m> {
233
let declaration = self.manager.declaration(self.reference.declaration);
234
DeclarationView {
235
- manager: &self.manager,
235
+ manager: self.manager,
236
declaration,
237
}
238
}
compiler/forget/crates/forget_ssa/src/enter.rs
+5
-5
@@ -38,9 +38,9 @@ pub fn enter_ssa_impl(
38
Ok(())
39
}
40
41
-fn visit_instructions<'e>(
41
+fn visit_instructions(
42
env: &Environment,
43
- builder: &mut Builder<'e>,
43
+ builder: &mut Builder<'_>,
44
hir: &mut HIR,
45
) -> Result<(), Diagnostic> {
46
let instructions = &mut hir.instructions;
@@ -175,7 +175,7 @@ impl<'e> Builder<'e> {
175
Ok(())
176
}
177
178
- fn visit_param(&mut self, param: &mut IdentifierOperand) -> () {
178
+ fn visit_param(&mut self, param: &mut IdentifierOperand) {
179
let old_identifier = ¶m.identifier;
180
let new_identifier = self.make_identifier(old_identifier);
181
let state = self.states.get_mut(&self.current).unwrap();
@@ -183,7 +183,7 @@ impl<'e> Builder<'e> {
183
param.identifier = new_identifier;
184
}
185
186
- fn visit_load(&mut self, local: &mut IdentifierOperand) -> () {
186
+ fn visit_load(&mut self, local: &mut IdentifierOperand) {
187
let new_identifier = self.get_id_at(self.current, &local.identifier);
188
local.identifier = new_identifier;
189
}
@@ -262,7 +262,7 @@ impl<'e> Builder<'e> {
262
}
263
}
264
265
- fn fix_incomplete_phis(&mut self, block_id: BlockId) -> () {
265
+ fn fix_incomplete_phis(&mut self, block_id: BlockId) {
266
let state = self.states.get_mut(&block_id).unwrap();
267
let incomplete_phis = std::mem::take(&mut state.incomplete_phis);
268
for phi in incomplete_phis {