diff --git a/src/docs/unchecked_duration_subtraction.txt b/src/docs/unchecked_duration_subtraction.txt new file mode 100644 index 00000000000..6b9c9308c87 --- /dev/null +++ b/src/docs/unchecked_duration_subtraction.txt @@ -0,0 +1,19 @@ +### What it does +Finds patterns of unchecked subtraction of [`Duration`] from [`Instant::now()`]. + +### Why is this bad? +Unchecked subtraction could cause underflow on certain platforms, leading to bugs and/or +unintentional panics. + +### Example +``` +let time_passed = Instant::now() - Duration::from_secs(5); +``` + +Use instead: +``` +let time_passed = Instant::now().checked_sub(Duration::from_secs(5)); +``` + +[`Duration`]: std::time::Duration +[`Instant::now()`]: std::time::Instant::now; \ No newline at end of file