From 474b23d6e726adeddd946e250030e0ac06f512a4 Mon Sep 17 00:00:00 2001 From: Benjamin Gordon Date: Wed, 5 Feb 2020 16:25:18 -0700 Subject: [PATCH] SC2191: Tighten index checks When adding a value containing an equals sign to an indexed array, the left side is treated as an index if it looks like [N]=val and N is numeric. SC2191 currently warns about anything that looks like key=val even though non-numeric values of key will never be treated as an index. This causes spurious warnings for the common pattern of building up program arguments in an array, such as: args=( --dry-run --in="${my_var}" --out=/some/path -f ) /bin/program "${args[@]}" Since only numeric expressions can be a valid index for an indexed array, only emit SC2191 if the left side of a literal string containing an equals looks numeric. Other more complicated constructs should still warn because shellcheck doesn't know if they may evaluate to a numeric result. Associative arrays still warn because a non-numeric left side is a valid subscript. --- src/ShellCheck/Analytics.hs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ShellCheck/Analytics.hs b/src/ShellCheck/Analytics.hs index acd62b1..d04d15e 100644 --- a/src/ShellCheck/Analytics.hs +++ b/src/ShellCheck/Analytics.hs @@ -3025,9 +3025,13 @@ prop_checkArrayAssignmentIndices3 = verifyNotTree checkArrayAssignmentIndices "d prop_checkArrayAssignmentIndices4 = verifyTree checkArrayAssignmentIndices "typeset -A foo; foo+=(bar)" prop_checkArrayAssignmentIndices5 = verifyTree checkArrayAssignmentIndices "arr=( [foo]= bar )" prop_checkArrayAssignmentIndices6 = verifyTree checkArrayAssignmentIndices "arr=( [foo] = bar )" -prop_checkArrayAssignmentIndices7 = verifyTree checkArrayAssignmentIndices "arr=( var=value )" +prop_checkArrayAssignmentIndices7 = verifyNotTree checkArrayAssignmentIndices "arr=( var=value )" prop_checkArrayAssignmentIndices8 = verifyNotTree checkArrayAssignmentIndices "arr=( [foo]=bar )" prop_checkArrayAssignmentIndices9 = verifyNotTree checkArrayAssignmentIndices "arr=( [foo]=\"\" )" +prop_checkArrayAssignmentIndices10 = verifyTree checkArrayAssignmentIndices "declare -A arr; arr=( var=value )" +prop_checkArrayAssignmentIndices11 = verifyTree checkArrayAssignmentIndices "arr=( 1=value )" +prop_checkArrayAssignmentIndices12 = verifyTree checkArrayAssignmentIndices "arr=( $a=value )" +prop_checkArrayAssignmentIndices13 = verifyTree checkArrayAssignmentIndices "arr=( $((1+1))=value )" checkArrayAssignmentIndices params root = runNodeAnalysis check params root where @@ -3052,7 +3056,7 @@ checkArrayAssignmentIndices params root = (id, str) <- case part of T_Literal id str -> [(id,str)] _ -> [] - guard $ '=' `elem` str + guard $ '=' `elem` str && hasNumericIndex str return $ warnWithFix id 2191 "The = here is literal. To assign by index, use ( [index]=value ) with no spaces. To keep as literal, quote it." (surroundWidth id params "\"") in if null literalEquals && isAssociative @@ -3060,6 +3064,9 @@ checkArrayAssignmentIndices params root = else sequence_ literalEquals _ -> return () + where + hasNumericIndex str = all isDigit $ takeWhile (/= '=') str + prop_checkUnmatchableCases1 = verify checkUnmatchableCases "case foo in bar) true; esac" prop_checkUnmatchableCases2 = verify checkUnmatchableCases "case foo-$bar in ??|*) true; esac"