Skip to content

Commit 3fa688b

Browse files
committed
fmt
1 parent eb2bbe9 commit 3fa688b

File tree

4 files changed

+100
-45
lines changed

4 files changed

+100
-45
lines changed

crates/filament/src/ast_passes/unused_elements.rs

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ struct ComponentUsage {
1212
invocations: HashMap<String, ast::Loc<fil_utils::Id>>,
1313
let_params: HashMap<String, ast::Loc<fil_utils::Id>>,
1414
sig_params: HashMap<String, ast::Loc<fil_utils::Id>>,
15-
15+
1616
// Usage tracking
1717
invoked_instances: HashSet<String>,
1818
accessed_invocations: HashSet<String>,
1919
referenced_params: HashSet<String>,
2020
}
2121

22-
2322
/// Check for unused elements in AST (instances, invocations, parameters)
2423
pub struct UnusedElementsCheck {
2524
diag: Diagnostics,
@@ -46,7 +45,10 @@ impl crate::ast_visitor::Construct for UnusedElementsCheck {
4645

4746
impl UnusedElementsCheck {
4847
fn current_usage(&mut self) -> &mut ComponentUsage {
49-
let comp_name = self.current_component.clone().expect("No current component");
48+
let comp_name = self
49+
.current_component
50+
.clone()
51+
.expect("No current component");
5052
self.component_usage.entry(comp_name).or_default()
5153
}
5254

@@ -63,11 +65,15 @@ impl UnusedElementsCheck {
6365
match expr {
6466
ast::Expr::Abstract(name) => {
6567
let usage = self.current_usage();
66-
usage.referenced_params.insert(name.inner().as_ref().to_string());
68+
usage
69+
.referenced_params
70+
.insert(name.inner().as_ref().to_string());
6771
}
6872
ast::Expr::ParamAccess { inst: _, param } => {
6973
let usage = self.current_usage();
70-
usage.referenced_params.insert(param.inner().as_ref().to_string());
74+
usage
75+
.referenced_params
76+
.insert(param.inner().as_ref().to_string());
7177
}
7278
ast::Expr::Op { left, right, .. } => {
7379
self.visit_expr_for_params(left);
@@ -88,12 +94,18 @@ impl UnusedElementsCheck {
8894
}
8995
}
9096

91-
fn visit_order_constraint_for_params(&mut self, constraint: &ast::OrderConstraint<Box<ast::Expr>>) {
97+
fn visit_order_constraint_for_params(
98+
&mut self,
99+
constraint: &ast::OrderConstraint<Box<ast::Expr>>,
100+
) {
92101
self.visit_expr_for_params(&constraint.left);
93102
self.visit_expr_for_params(&constraint.right);
94103
}
95104

96-
fn visit_order_constraint_unboxed_for_params(&mut self, constraint: &ast::OrderConstraint<ast::Expr>) {
105+
fn visit_order_constraint_unboxed_for_params(
106+
&mut self,
107+
constraint: &ast::OrderConstraint<ast::Expr>,
108+
) {
97109
self.visit_expr_for_params(&constraint.left);
98110
self.visit_expr_for_params(&constraint.right);
99111
}
@@ -106,9 +118,17 @@ impl UnusedElementsCheck {
106118
}
107119
}
108120

109-
fn add_unused_warning(&mut self, element_type: &str, description: &str, loc: GPosIdx) {
121+
fn add_unused_warning(
122+
&mut self,
123+
element_type: &str,
124+
description: &str,
125+
loc: GPosIdx,
126+
) {
110127
let mut warning = Error::unused_element(element_type, description)
111-
.add_note(self.diag.add_info(format!("{} defined here", element_type), loc));
128+
.add_note(
129+
self.diag
130+
.add_info(format!("{} defined here", element_type), loc),
131+
);
112132

113133
// Promote to error if flag is set
114134
if self.warnings_as_errors {
@@ -131,28 +151,44 @@ impl UnusedElementsCheck {
131151
// Check unused instances
132152
for (inst_name, def_loc) in &usage.instances {
133153
if !usage.invoked_instances.contains(inst_name) {
134-
warnings.push((def_loc.pos(), "instance", "instance is created but never invoked"));
154+
warnings.push((
155+
def_loc.pos(),
156+
"instance",
157+
"instance is created but never invoked",
158+
));
135159
}
136160
}
137161

138162
// Check unused invocations (no ports accessed)
139163
for (inv_name, def_loc) in &usage.invocations {
140164
if !usage.accessed_invocations.contains(inv_name) {
141-
warnings.push((def_loc.pos(), "invocation", "output ports on invocation never used"));
165+
warnings.push((
166+
def_loc.pos(),
167+
"invocation",
168+
"output ports on invocation never used",
169+
));
142170
}
143171
}
144172

145173
// Check unused let parameters
146174
for (param_name, def_loc) in &usage.let_params {
147175
if !usage.referenced_params.contains(param_name) {
148-
warnings.push((def_loc.pos(), "parameter", "parameter is defined but never used"));
176+
warnings.push((
177+
def_loc.pos(),
178+
"parameter",
179+
"parameter is defined but never used",
180+
));
149181
}
150182
}
151183

152184
// Check unused signature parameters
153185
for (param_name, def_loc) in &usage.sig_params {
154186
if !usage.referenced_params.contains(param_name) {
155-
warnings.push((def_loc.pos(), "parameter", "parameter is defined but never used"));
187+
warnings.push((
188+
def_loc.pos(),
189+
"parameter",
190+
"parameter is defined but never used",
191+
));
156192
}
157193
}
158194

@@ -190,17 +226,16 @@ impl Visitor for UnusedElementsCheck {
190226
fn signature(&mut self, sig: &mut ast::Signature) -> Action {
191227
let comp_name = sig.name.inner().as_ref().to_string();
192228
self.current_component = Some(comp_name.clone());
193-
229+
194230
// Record signature parameters
195231
let usage = self.current_usage();
196232
for param in &sig.params {
197-
usage.sig_params.insert(
198-
param.name().as_ref().to_string(),
199-
param.param.clone(),
200-
);
233+
usage
234+
.sig_params
235+
.insert(param.name().as_ref().to_string(), param.param.clone());
201236
}
202237

203-
// Record let-bound parameters from the 'with' section
238+
// Record let-bound parameters from the 'with' section
204239
// First pass: record parameters
205240
for sig_bind in &sig.sig_bindings {
206241
match sig_bind.inner() {
@@ -227,7 +262,9 @@ impl Visitor for UnusedElementsCheck {
227262
}
228263
ast::SigBind::Exists { cons, .. } => {
229264
for constraint in cons {
230-
self.visit_order_constraint_unboxed_for_params(constraint.inner());
265+
self.visit_order_constraint_unboxed_for_params(
266+
constraint.inner(),
267+
);
231268
}
232269
}
233270
}
@@ -239,10 +276,9 @@ impl Visitor for UnusedElementsCheck {
239276

240277
fn instance(&mut self, inst: &mut ast::Instance) -> Action {
241278
let usage = self.current_usage();
242-
usage.instances.insert(
243-
inst.name.inner().as_ref().to_string(),
244-
inst.name.clone(),
245-
);
279+
usage
280+
.instances
281+
.insert(inst.name.inner().as_ref().to_string(), inst.name.clone());
246282

247283
// Visit parameter expressions for parameter usage
248284
for param_expr in &inst.params {
@@ -254,17 +290,16 @@ impl Visitor for UnusedElementsCheck {
254290

255291
fn invoke(&mut self, inv: &mut ast::Invoke) -> Action {
256292
let usage = self.current_usage();
257-
293+
258294
// Record invocation definition
259-
usage.invocations.insert(
260-
inv.name.inner().as_ref().to_string(),
261-
inv.name.clone(),
262-
);
263-
295+
usage
296+
.invocations
297+
.insert(inv.name.inner().as_ref().to_string(), inv.name.clone());
298+
264299
// Mark the instance as being invoked
265-
usage.invoked_instances.insert(
266-
inv.instance.inner().as_ref().to_string()
267-
);
300+
usage
301+
.invoked_instances
302+
.insert(inv.instance.inner().as_ref().to_string());
268303

269304
// Visit port expressions for parameter usage
270305
for port in &inv.ports {

crates/filament/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fn run(opts: &cmdline::Opts) -> Result<(), u64> {
125125
};
126126

127127
ast_pass_pipeline! { opts, ns; ap::TopLevel };
128-
128+
129129
// Run unused elements check only if warnings are not disabled
130130
if !opts.no_warn_unused {
131131
ast_pass_pipeline! { opts, ns; ap::UnusedElementsCheck };

crates/utils/src/errors.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,16 @@ impl Error {
9999
}
100100
}
101101

102-
pub fn unused_element<S: ToString>(element_type: S, description: S) -> Self {
102+
pub fn unused_element<S: ToString>(
103+
element_type: S,
104+
description: S,
105+
) -> Self {
103106
Self {
104-
kind: format!("unused {}: {}", element_type.to_string(), description.to_string()),
107+
kind: format!(
108+
"unused {}: {}",
109+
element_type.to_string(),
110+
description.to_string()
111+
),
105112
notes: vec![],
106113
severity: Severity::Warning,
107114
}

crates/utils/src/reporter.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ impl Diagnostics {
9393

9494
/// Add a warning to the diagnostics instance.
9595
pub fn add_warning(&mut self, warning: Error) {
96-
let warning = Error { severity: Severity::Warning, ..warning };
96+
let warning = Error {
97+
severity: Severity::Warning,
98+
..warning
99+
};
97100
self.add_error(warning);
98101
}
99102

@@ -106,16 +109,17 @@ impl Diagnostics {
106109
} else {
107110
ColorChoice::Never
108111
});
109-
112+
110113
// Take ownership to avoid borrowing issues
111114
let warnings = std::mem::take(&mut self.warnings);
112115
let errors = std::mem::take(&mut self.errors);
113-
116+
114117
// Report warnings first
115118
self.report_diagnostics_owned(&writer, warnings, Severity::Warning);
116-
119+
117120
// Report errors and return count
118-
let error_count = self.report_diagnostics_owned(&writer, errors, Severity::Error);
121+
let error_count =
122+
self.report_diagnostics_owned(&writer, errors, Severity::Error);
119123

120124
if error_count > 0 {
121125
Some(error_count)
@@ -124,7 +128,12 @@ impl Diagnostics {
124128
}
125129
}
126130

127-
fn report_diagnostics_owned(&self, writer: &StandardStream, mut diagnostics: Vec<Error>, severity: Severity) -> u64 {
131+
fn report_diagnostics_owned(
132+
&self,
133+
writer: &StandardStream,
134+
mut diagnostics: Vec<Error>,
135+
severity: Severity,
136+
) -> u64 {
128137
if diagnostics.is_empty() {
129138
return 0;
130139
}
@@ -173,8 +182,12 @@ impl Diagnostics {
173182
let msg = if messages.len() > 1 {
174183
notes.extend(messages.iter().map(|e| e.to_string()));
175184
match severity {
176-
Severity::Error => "Multiple errors encountered".to_string(),
177-
Severity::Warning => "Multiple warnings encountered".to_string(),
185+
Severity::Error => {
186+
"Multiple errors encountered".to_string()
187+
}
188+
Severity::Warning => {
189+
"Multiple warnings encountered".to_string()
190+
}
178191
}
179192
} else {
180193
messages[0].to_string()
@@ -185,7 +198,7 @@ impl Diagnostics {
185198
Severity::Error => Diagnostic::error(),
186199
Severity::Warning => Diagnostic::warning(),
187200
};
188-
201+
189202
term::emit(
190203
&mut writer.lock(),
191204
&term::Config::default(),

0 commit comments

Comments
 (0)