Skip to content

Commit 72a1796

Browse files
committed
Auto merge of rust-lang#14819 - weihanglo:divided-by-zero, r=lnicola
fix(analysis-stats): divided by zero error ## What does this PR try to resolve? 2023-05-15 rust-analyzer suffers from ``` thread 'main' panicked at 'attempt to divide by zero', crates/rust-analyzer/src/cli/analysis_stats.rs:230:56 ``` This commit <rust-lang/rust-analyzer@51e8b8f> might be the culprit. This PR uses `percentage` function to avoid the classic “division by zero” bug. ## Reproducer ```console cargo new ra-test pushd ra-test echo "pub type Foo = u32;" >> src/lib.rs rust-analyzer analysis-stats . ```
2 parents d0768aa + 77be56b commit 72a1796

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

crates/rust-analyzer/src/cli/analysis_stats.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl flags::AnalysisStats {
227227
fail += 1;
228228
}
229229
eprintln!("{:<20} {}", "Data layouts:", sw.elapsed());
230-
eprintln!("Failed data layouts: {fail} ({}%)", fail * 100 / all);
230+
eprintln!("Failed data layouts: {fail} ({}%)", percentage(fail, all));
231231
report_metric("failed data layouts", fail, "#");
232232
}
233233

@@ -254,7 +254,7 @@ impl flags::AnalysisStats {
254254
fail += 1;
255255
}
256256
eprintln!("{:<20} {}", "Const evaluation:", sw.elapsed());
257-
eprintln!("Failed const evals: {fail} ({}%)", fail * 100 / all);
257+
eprintln!("Failed const evals: {fail} ({}%)", percentage(fail, all));
258258
report_metric("failed const evals", fail, "#");
259259
}
260260

@@ -280,7 +280,7 @@ impl flags::AnalysisStats {
280280
fail += 1;
281281
}
282282
eprintln!("{:<20} {}", "MIR lowering:", sw.elapsed());
283-
eprintln!("Mir failed bodies: {fail} ({}%)", fail * 100 / all);
283+
eprintln!("Mir failed bodies: {fail} ({}%)", percentage(fail, all));
284284
report_metric("mir failed bodies", fail, "#");
285285
}
286286

0 commit comments

Comments
 (0)