Skip to content

Commit 1cc6f03

Browse files
authored
Merge pull request #1073 from jaytaph/linting
Added linting checks on all crates
2 parents c9397b6 + dd80efc commit 1cc6f03

48 files changed

Lines changed: 396 additions & 304 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "gosub_bin"
33
version = "0.1.1"
44
edition = "2021"
5-
rust-version = "1.80"
5+
rust-version = "1.82"
66
authors = ["Gosub Community <info@gosub.io>"]
77
description = "Gosub Browser Engine"
88
license = "MIT"

crates/gosub_config/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ cow-utils = { workspace = true }
2424
rusqlite = "0.39.0"
2525

2626
[dev-dependencies]
27-
testing_logger = "0.1.1"
27+
testing_logger = "0.1.1"
28+
[lints]
29+
workspace = true

crates/gosub_css3/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ indexmap = { version = "2.13.0", optional = true }
2626
[features]
2727
default = []
2828
unresolved_syntax = ["dep:indexmap"]
29+
30+
[lints]
31+
workspace = true

crates/gosub_css3/src/ast.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,13 @@ fn collect_rule(node: &CssNode) -> CssResult<Option<CssRule>> {
182182
continue;
183183
}
184184

185-
let value = if css_values.len() == 1 {
186-
css_values.pop().expect("unreachable")
187-
} else {
188-
CssValue::List(css_values)
185+
let value = match css_values.pop() {
186+
Some(value) if css_values.is_empty() => value,
187+
Some(value) => {
188+
css_values.push(value);
189+
CssValue::List(css_values)
190+
}
191+
None => CssValue::List(css_values),
189192
};
190193

191194
rule.declarations.push(CssDeclaration {
@@ -294,7 +297,7 @@ mod tests {
294297

295298
#[test]
296299
fn convert_font_family() {
297-
let stylesheet = Css3::parse_str(
300+
let _stylesheet = Css3::parse_str(
298301
r#"
299302
body {
300303
border: 1px solid black;
@@ -310,8 +313,6 @@ mod tests {
310313
"test.css",
311314
)
312315
.unwrap();
313-
314-
dbg!(&stylesheet);
315316
}
316317

317318
#[test]

crates/gosub_css3/src/colors.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,37 +62,31 @@ impl From<&str> for RgbColor {
6262
}
6363
if value.starts_with("rgb(") {
6464
// Rgb function
65-
let rgb = Rgb::from_str(value);
66-
if rgb.is_err() {
65+
let Ok(rgb) = Rgb::from_str(value) else {
6766
return RgbColor::default();
68-
}
69-
let rgb = rgb.unwrap();
67+
};
7068
return RgbColor::new(rgb.get_red(), rgb.get_green(), rgb.get_blue(), 255.0);
7169
}
7270
if value.starts_with("rgba(") {
7371
// Rgba function — alpha from colors_transform is in 0..1 range; scale to 0..255
74-
let rgb = Rgb::from_str(value);
75-
if rgb.is_err() {
72+
let Ok(rgb) = Rgb::from_str(value) else {
7673
return RgbColor::default();
77-
}
78-
let rgb = rgb.unwrap();
74+
};
7975
return RgbColor::new(rgb.get_red(), rgb.get_green(), rgb.get_blue(), rgb.get_alpha() * 255.0);
8076
}
8177
if value.starts_with("hsl(") {
82-
let hsl = Hsl::from_str(value);
83-
if hsl.is_err() {
78+
let Ok(hsl) = Hsl::from_str(value) else {
8479
return RgbColor::default();
85-
}
86-
let rgb: Rgb = hsl.unwrap().to_rgb();
80+
};
81+
let rgb: Rgb = hsl.to_rgb();
8782
return RgbColor::new(rgb.get_red(), rgb.get_green(), rgb.get_blue(), 255.0);
8883
}
8984
if value.starts_with("hsla(") {
9085
// hsla() — alpha from colors_transform is in 0..1 range; scale to 0..255
91-
let hsl = Hsl::from_str(value);
92-
if hsl.is_err() {
86+
let Ok(hsl) = Hsl::from_str(value) else {
9387
return RgbColor::default();
94-
}
95-
let rgb: Rgb = hsl.unwrap().to_rgb();
88+
};
89+
let rgb: Rgb = hsl.to_rgb();
9690
return RgbColor::new(rgb.get_red(), rgb.get_green(), rgb.get_blue(), rgb.get_alpha() * 255.0);
9791
}
9892

@@ -307,15 +301,14 @@ fn parse_hex(value: &str) -> RgbColor {
307301
fn convert_from_hex_str_to_vec_of_ints(hex_value: &str, hex_size: usize) -> Vec<i32> {
308302
const HEX_RADIX: u32 = 16;
309303
const LINES_TO_SKIP: usize = 1;
310-
const EXPECT_MESSAGE: &str = "is_hex has failed us";
311304
// Get the individual chars from the hex then convert from hex -> decimal
312305
match hex_size {
313306
// if each hex char is only 1 char long
314307
1 => {
315308
hex_value
316309
.chars()
317310
.skip(LINES_TO_SKIP) // Skip the # at the front
318-
.map(|char| i32::from_str_radix(char.to_string().as_str(), HEX_RADIX).expect(EXPECT_MESSAGE)) // Can parse with an unwrap because is_hex made sure it's digits
311+
.map(|char| i32::from_str_radix(char.to_string().as_str(), HEX_RADIX).unwrap_or(0)) // is_hex() above guarantees digits
319312
.collect::<Vec<i32>>()
320313
}
321314
// if each hex char is 2 char long
@@ -331,7 +324,7 @@ fn convert_from_hex_str_to_vec_of_ints(hex_value: &str, hex_size: usize) -> Vec<
331324

332325
hex_vec
333326
.iter()
334-
.map(|str| i32::from_str_radix(str, HEX_RADIX).expect(EXPECT_MESSAGE))
327+
.map(|str| i32::from_str_radix(str, HEX_RADIX).unwrap_or(0))
335328
.collect::<Vec<i32>>()
336329
}
337330
_ => {

crates/gosub_css3/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pub mod errors;
2020
mod functions;
2121
#[allow(dead_code)]
2222
pub mod matcher;
23+
// The as_* accessors panic by contract when called on the wrong node type;
24+
// callers are expected to check the matching is_* predicate first.
25+
#[allow(clippy::panic)]
2326
pub mod node;
2427
pub mod parser;
2528
pub mod stylesheet;
@@ -119,6 +122,7 @@ pub fn load_default_useragent_stylesheet() -> CssStylesheet {
119122
};
120123

121124
let css_data = include_str!("../resources/useragent.css");
125+
#[allow(clippy::expect_used)] // PANIC-SAFE: compiled-in stylesheet, exercised by every parser test
122126
Css3::parse_str(css_data, config, CssOrigin::UserAgent, url).expect("Could not parse useragent stylesheet")
123127
}
124128

crates/gosub_css3/src/matcher/property_definitions.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,11 @@ impl CssDefinitions {
249249
return;
250250
}
251251

252-
if !self.properties.contains_key(name) {
252+
// Resolve the element
253+
let Some(mut element) = self.properties.get(name).cloned() else {
253254
// Property not found to resolve
254255
return;
255-
}
256-
257-
// Resolve the element
258-
let mut element = self.properties.get_mut(name).unwrap().clone();
256+
};
259257
let mut resolved_components = vec![];
260258
for component in &element.syntax.components {
261259
let component = self.resolve_component(component, name);
@@ -329,7 +327,11 @@ impl CssDefinitions {
329327
};
330328
}
331329

332-
panic!("Unknown datatype encountered: {datatype:?}");
330+
#[allow(clippy::panic)]
331+
// PANIC-SAFE: datatypes come from the compiled-in definitions; the test suite resolves them all
332+
{
333+
panic!("Unknown datatype encountered: {datatype:?}");
334+
}
333335
}
334336
SyntaxComponent::Group {
335337
components,
@@ -379,11 +381,13 @@ pub static CSS_PROPERTIES: LazyLock<indexmap::IndexMap<String, PropertyDefinitio
379381
pub const DEFINITIONS_VALUES: &str = include_str!("../../resources/definitions/definitions_values.json");
380382
pub const DEFINITIONS_PROPERTIES: &str = include_str!("../../resources/definitions/definitions_properties.json");
381383

384+
#[allow(clippy::expect_used)] // PANIC-SAFE: compiled-in definitions file, validated by the test suite
382385
fn get_values<M: Map<String, SyntaxDefinition>>() -> M {
383386
let json: serde_json::Value = serde_json::from_str(DEFINITIONS_VALUES).expect("JSON was not well-formatted");
384387
parse_syntax_file(json)
385388
}
386389

390+
#[allow(clippy::expect_used)] // PANIC-SAFE: compiled-in definitions file, validated by the test suite
387391
fn get_properties<M: Map<String, PropertyDefinition>>() -> M {
388392
let json: serde_json::Value = serde_json::from_str(DEFINITIONS_PROPERTIES).expect("JSON was not well-formatted");
389393
parse_property_file(json)
@@ -455,6 +459,7 @@ impl<K: Eq + Hash, V> Map<K, V> for indexmap::IndexMap<K, V> {
455459
}
456460

457461
/// Parses a syntax JSON import file
462+
#[allow(clippy::unwrap_used)] // PANIC-SAFE: parses the compiled-in definitions; validated by the test suite
458463
fn parse_syntax_file<M: Map<String, SyntaxDefinition>>(json: serde_json::Value) -> M {
459464
let mut syntaxes = M::new();
460465

@@ -517,6 +522,7 @@ fn parse_syntax_file<M: Map<String, SyntaxDefinition>>(json: serde_json::Value)
517522
}
518523

519524
/// Parses the JSON input into a CSS property definitions structure
525+
#[allow(clippy::unwrap_used, clippy::panic)] // PANIC-SAFE: parses the compiled-in definitions; validated by the test suite
520526
fn parse_property_file<M: Map<String, PropertyDefinition>>(json: serde_json::Value) -> M {
521527
let mut properties = M::new();
522528

crates/gosub_css3/src/matcher/shorthands.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,7 @@ impl CssDefinitions {
575575
}
576576
}
577577

578-
if syntax.components.len() == 1 {
579-
let component = syntax.components.first().unwrap();
580-
578+
if let [component] = syntax.components.as_slice() {
581579
match component {
582580
SyntaxComponent::Definition { datatype, .. } => {
583581
if let Some(d) = self.syntax.get(datatype) {
@@ -731,8 +729,6 @@ mod tests {
731729
current_info: None
732730
}
733731
);
734-
735-
dbg!(fix_list);
736732
}
737733

738734
#[test]
@@ -752,21 +748,15 @@ mod tests {
752748
&mut fix_list,
753749
));
754750

755-
dbg!(&fix_list);
756-
757751
fix_list.resolve_nested(definitions);
758752

759-
dbg!(&fix_list);
760-
761753
fix_list = FixList::new();
762754

763755
assert!(prop.clone().matches_and_shorthands(
764756
&[str!("solid"), CssValue::Color(RgbColor::new(0.0, 0.0, 0.0, 0.0))],
765757
&mut fix_list,
766758
));
767759

768-
dbg!(fix_list);
769-
770760
fix_list = FixList::new();
771761

772762
assert!(prop.clone().matches_and_shorthands(
@@ -777,7 +767,5 @@ mod tests {
777767
],
778768
&mut fix_list,
779769
));
780-
781-
dbg!(fix_list);
782770
}
783771
}

crates/gosub_css3/src/matcher/syntax.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ fn parse_unit(input: &str) -> IResult<&str, SyntaxComponent> {
284284
let (input, value) = float(input)?;
285285
let (input, suffix) = opt(alpha1).parse(input)?;
286286

287-
if suffix.is_none() {
287+
let Some(suffix) = suffix else {
288288
return if value == 0.0 {
289289
// 0 is a special case as it doesn't need a unit
290290
Ok((
@@ -297,12 +297,12 @@ fn parse_unit(input: &str) -> IResult<&str, SyntaxComponent> {
297297
} else {
298298
Err(Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Alpha)))
299299
};
300-
}
300+
};
301301

302302
Ok((
303303
input,
304304
SyntaxComponent::Value {
305-
value: CssValue::Unit(value, suffix.unwrap().to_string()),
305+
value: CssValue::Unit(value, suffix.to_string()),
306306
multipliers: vec![SyntaxComponentMultiplier::Once],
307307
},
308308
))
@@ -512,13 +512,11 @@ fn juxtaposition_or_separated_list(input: &str) -> IResult<&str, Vec<SyntaxCompo
512512
// Check for a separator
513513
let result = juxtaseparator(next_input);
514514

515-
// If errored, we return what we've got
516-
if result.is_err() {
517-
return Ok((next_input, elements));
518-
}
519-
515+
// If errored, we return what we've got.
520516
// If found, the sep boolean determines if we are done or not.
521-
let (next_input, sep) = result.unwrap();
517+
let Ok((next_input, sep)) = result else {
518+
return Ok((next_input, elements));
519+
};
522520
if !sep {
523521
return Ok((next_input, elements));
524522
}
@@ -560,7 +558,7 @@ fn parse_unit_inner(input: &str) -> IResult<&str, SyntaxComponent> {
560558
let (input, _) = multispace0(input)?;
561559
let (input, suffixes) = opt(separated_list0(ws(tag("|")), alpha1)).parse(input)?;
562560

563-
if suffixes.is_none() {
561+
let Some(suffixes) = suffixes else {
564562
// No suffixes, just a range
565563
return Ok((
566564
input,
@@ -571,10 +569,10 @@ fn parse_unit_inner(input: &str) -> IResult<&str, SyntaxComponent> {
571569
multipliers: vec![SyntaxComponentMultiplier::Once],
572570
},
573571
));
574-
}
572+
};
575573

576574
// Convert the suffixes to a vector of strings
577-
let suffixes: Vec<String> = suffixes.unwrap().iter().map(|s| (*s).to_string()).collect();
575+
let suffixes: Vec<String> = suffixes.iter().map(|s| (*s).to_string()).collect();
578576
Ok((
579577
input,
580578
SyntaxComponent::Unit {

0 commit comments

Comments
 (0)