Skip to content

Commit

Permalink
ban Ord::{min, max} with Clippy as it's too easy to misread
Browse files Browse the repository at this point in the history
It is *much* too easy to misread `x.min(y)` as "`x` should be *at least* `y`" when in fact it means the *exact* opposite, and same with `x.max(y)`. This has bitten us in the gluteus maximus a number of times both in SQLx and in private projects.

Signed-off-by: Austin Bonander <[email protected]>
  • Loading branch information
abonander committed Feb 5, 2021
1 parent cbe8207 commit 1a9e0d9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
disallowed-methods = [
# It is *much* too easy to misread `x.min(y)` as "x should be *at least* y" when in fact it
# means the *exact* opposite, and same with `x.max(y)`; use `cmp::{min, max}` instead.
"core::cmp::Ord::min", "core::cmp::Ord::max"
]
2 changes: 2 additions & 0 deletions sqlx-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#![recursion_limit = "512"]
#![warn(future_incompatible, rust_2018_idioms)]
#![allow(clippy::needless_doctest_main, clippy::type_complexity)]
// See `clippy.toml` at the workspace root
#![deny(clippy::disallowed_method)]
//
// Allows an API be documented as only available in some specific platforms.
// <https://doc.rust-lang.org/unstable-book/language-features/doc-cfg.html>
Expand Down
3 changes: 2 additions & 1 deletion sqlx-core/src/pool/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::pool::inner::SharedPool;
use crate::pool::Pool;
use futures_core::future::BoxFuture;
use sqlx_rt::spawn;
use std::cmp;
use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -228,7 +229,7 @@ impl<DB: Database> PoolOptions<DB> {
}

async fn init_min_connections<DB: Database>(pool: &SharedPool<DB>) -> Result<(), Error> {
for _ in 0..pool.options.min_connections.max(1) {
for _ in 0..cmp::max(pool.options.min_connections, 1) {
let deadline = Instant::now() + pool.options.connect_timeout;

// this guard will prevent us from exceeding `max_size`
Expand Down

0 comments on commit 1a9e0d9

Please sign in to comment.