SC2256: Check for translated strings matching known variables

SC2247 already warns about translated strings that look like $"(foo)" or
$"{foo}".  Since typical use of translated strings is to translate whole
messages, a string like $"foo" is likely to be a similar mistake if foo
is the name of an existing variable.  Conversely, a string like
$"foo bar" is potentially meant to be a message id even if foo is a
known variable.

Add a warning for the $"foo" case, but make it separate from the
existing warning so that projects that reuse variable names as their
message ids can separately disable the new warning.
This commit is contained in:
Benjamin Gordon 2019-11-13 15:50:21 -07:00
parent 93eca1cb8e
commit 2341a4c683
2 changed files with 20 additions and 0 deletions

View File

@ -6,6 +6,7 @@
### Added ### Added
- SC2254: Suggest quoting expansions in case statements - SC2254: Suggest quoting expansions in case statements
- SC2255: Suggest using `$((..))` in `[ 2*3 -eq 6 ]` - SC2255: Suggest using `$((..))` in `[ 2*3 -eq 6 ]`
- SC2256: Warn about translated strings that are known variables
## v0.7.0 - 2019-07-28 ## v0.7.0 - 2019-07-28
### Added ### Added

View File

@ -192,6 +192,7 @@ nodeChecks = [
,checkRedirectionToCommand ,checkRedirectionToCommand
,checkDollarQuoteParen ,checkDollarQuoteParen
,checkUselessBang ,checkUselessBang
,checkTranslatedString
] ]
optionalChecks = map fst optionalTreeChecks optionalChecks = map fst optionalTreeChecks
@ -3451,6 +3452,24 @@ checkDollarQuoteParen params t =
where where
fix id = fixWith [replaceStart id params 2 "\"$"] fix id = fixWith [replaceStart id params 2 "\"$"]
prop_checkTranslatedString1 = verify checkTranslatedString "foo_bar2=val; $\"foo_bar2\""
prop_checkTranslatedString2 = verifyNot checkTranslatedString "$\"foo_bar2\""
prop_checkTranslatedString3 = verifyNot checkTranslatedString "$\"..\""
prop_checkTranslatedString4 = verifyNot checkTranslatedString "var=val; $\"$var\""
prop_checkTranslatedString5 = verifyNot checkTranslatedString "foo=var; bar=val2; $\"foo bar\""
checkTranslatedString params (T_DollarDoubleQuoted id ((T_Literal _ s):_)) =
fromMaybe (return ()) $ do
Map.lookup s assignments
return $
warnWithFix id 2256 "This translated string is the name of a variable. Flip leading $ and \" if this should be a quoted substitution." (fix id)
where
assignments = foldl (flip ($)) Map.empty (map insertAssignment $ variableFlow params)
insertAssignment (Assignment (_, token, name, _)) | isVariableName name =
Map.insert name token
insertAssignment _ = Prelude.id
fix id = fixWith [replaceStart id params 2 "\"$"]
checkTranslatedString _ _ = return ()
prop_checkDefaultCase1 = verify checkDefaultCase "case $1 in a) true ;; esac" prop_checkDefaultCase1 = verify checkDefaultCase "case $1 in a) true ;; esac"
prop_checkDefaultCase2 = verify checkDefaultCase "case $1 in ?*?) true ;; *? ) true ;; esac" prop_checkDefaultCase2 = verify checkDefaultCase "case $1 in ?*?) true ;; *? ) true ;; esac"
prop_checkDefaultCase3 = verifyNot checkDefaultCase "case $1 in x|*) true ;; esac" prop_checkDefaultCase3 = verifyNot checkDefaultCase "case $1 in x|*) true ;; esac"