#!/usr/bin/env bash
#
# Checks code with clippy linter
#

set -eu

get_lint_args() {
    local action="$1"
    local lints="$2"

    sed -r '
    s/\s*#.*//
    /^\s*$/d
    s/^\s*(.*)/'"$action"' clippy::\1/
    ' <<< "$lints"
}

check() {
    local args="$1"
    local lints="$2"
    cargo clippy $args --all-features -- -Dwarnings $(get_lint_args -A "$lints")
}

main() {
    local blacklist

    # A workaround to force recheck
    touch src/main.rs

    blacklist='
        collapsible_else_if
        redundant_field_names
        new-ret-no-self
    '
    check --bins "$blacklist"
    check '--bins --release' "$blacklist"
    check --tests "$blacklist"

    cargo test
}

main
