#!/bin/sh
# smarm pre-commit gate: clippy the library (src/) with warnings as errors.
# unwrap_used / expect_used are denied (Cargo.toml [lints.clippy]): library
# code must not hide a panic behind unwrap/expect. Tests/examples are not gated.
#
# Toolchain resolution: prefer an installed cargo-clippy; on machines whose
# rust comes without the clippy component (e.g. NixOS home-manager), fall
# back to an ephemeral nix-shell toolchain. The fallback uses its own target
# dir (target/clippy) because the shell's rustc version may differ from the
# default toolchain's — mixed-compiler artifacts in one target dir are an
# E0514 hard error. MSRV (Cargo.toml rust-version) keeps the older shell
# toolchain a legitimate gate.
set -eu
[ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env"
cd "$(git rev-parse --show-toplevel)"
if cargo clippy --version >/dev/null 2>&1; then
    cargo clippy --lib -- -D warnings
elif command -v nix-shell >/dev/null 2>&1; then
    nix-shell -p clippy -p cargo -p rustc \
        --run 'CARGO_TARGET_DIR=target/clippy cargo clippy --lib -- -D warnings'
else
    echo "pre-commit: cargo clippy unavailable and no nix-shell fallback" >&2
    exit 1
fi
