Commit Graph

297 Commits

Author SHA1 Message Date
Vidar Holen cc3884cf9f Support env -S/--split-string in shebangs (fixes #2105) 2020-12-12 20:24:32 -08:00
Vidar Holen 6ba1af0898 Warn when a variable is assigned to itself 2020-12-11 20:28:36 -08:00
Vidar Holen 65044c2568 SC2095: Also warn if the command is backgrounded 2020-11-29 13:01:23 -08:00
Keith Smiley 619662adb6 Add readonly to SC2155
This adds a warning for readonly masking the return value of function.
This is mentioned in the wiki
https://github.com/koalaman/shellcheck/wiki/SC2155#problematic-code-in-the-case-of-readonly
but didn't actually produce a warning.

Fixes https://github.com/koalaman/shellcheck/issues/1336
2020-10-23 17:29:04 -07:00
Vidar Holen 28d3279ba6 Optional style warning about [ x$var = xval ] 2020-10-19 20:04:58 -07:00
Vidar Holen 256457c47a Use getopts parser to find 'read' arrays (fixes #2073) 2020-10-18 22:57:16 -07:00
Vidar Holen f100c2939e Rewrite getopts style option parser 2020-10-18 21:34:58 -07:00
Vidar Holen 8d99926554 Recognize `local -x` similarly to `export` (fixes #2069) 2020-10-18 15:15:31 -07:00
Vidar Holen 218deb6d01 Update SC2091/SC2092 message and ignore in quotes. 2020-09-08 19:30:13 -07:00
Vidar Holen 43191fa71d Suppress SC2035 for echo * and printf * (fixes #2036) 2020-09-01 14:19:28 -07:00
Vidar Holen a62d9f10c2 Warn when using &/| between test statements 2020-08-23 15:43:33 -07:00
Vidar Holen 50067ddf94 Consider variables in -z/-n tests to be checked 2020-08-08 12:32:20 -07:00
Vidar Holen 3fa5b7d3bd Merge branch 'supportMinusNZ' of https://github.com/donnerpeter/shellcheck into donnerpeter-supportMinusNZ 2020-08-08 11:22:00 -07:00
Vidar Holen e779aedac3 Modernize getting mapfile array name 2020-08-07 16:41:18 -07:00
Vidar Holen 3ef1175566 Merge branch 'issue_1759_mapfile_proc_substition' of https://github.com/Gandalf-/shellcheck into Gandalf--issue_1759_mapfile_proc_substition 2020-08-07 15:57:59 -07:00
Vidar Holen 3e50a2fce8 Suppress SC2216 for du --files0-from or --exclude-from (fixes #1286) 2020-08-07 14:59:34 -07:00
Vidar Holen 1ac2c31728 Warn when shell functions blatantly recurse (fixes #1994) 2020-07-27 21:50:33 -07:00
Vidar Holen 34885142e7 Handle tilde expansion in pattern matching (fixes #1769) 2020-07-27 18:34:42 -07:00
Vidar Holen 5d753212fb Improve handling of command prefixes like exec/command (fixes #2008) 2020-07-25 13:45:05 -07:00
Vidar Holen 5cf2c00ff7 Warn about defining and using an alias in a single command (fixes #1807) 2020-05-25 23:24:33 -07:00
Vidar Holen a08ad3bee9 Count $# as an argument reference in SC2120 2020-05-25 23:24:33 -07:00
Vidar Holen d6adbfde78 Improve SC2259/60/61 messages 2020-05-03 21:46:16 -07:00
Vidar Holen 2030b83607 Warn about duplicate uses of stdin/out/err 2020-05-03 11:54:25 -07:00
Vidar Holen 73cc11fd0a
Merge pull request #1901 from josephcsible/bracedstring
Mostly get rid of bracedString
2020-04-12 15:14:50 -07:00
Joseph C. Sible b58bb4ba9d Move bracedString to be local to its last use site 2020-04-11 19:24:11 -04:00
Joseph C. Sible 999b7e2596 Get rid of bracedString everywhere it's easy to 2020-04-11 19:24:11 -04:00
Joseph C. Sible 8a7497c4f0 Simplify checkVariableBraces 2020-04-11 19:23:13 -04:00
Joseph C. Sible 322842b57e Remove unnecessary monadicity from wordToPseudoGlob 2020-04-05 19:29:40 -04:00
Vidar Holen 01f4423465 Disable SC2257 about > $((i=42)) for Dash 2020-04-05 11:38:22 -07:00
Vidar Holen bd717c9d1b Don't warn about [ 0 -ne $FOO ] || [ 0 -ne $BAR ] (fixes #1891) 2020-04-01 22:09:00 -07:00
Vidar Holen da0931740f
Merge pull request #1876 from fork-graveyard/master
recognize `: ${parameter=word}` as assignment
2020-04-01 18:52:53 -07:00
Vidar Holen c60323fb25
Merge pull request #1873 from josephcsible/checkwhilereadpitfalls
Simplify checkWhileReadPitfalls
2020-03-31 19:10:53 -07:00
girst 7a5e261d03 recognize `: ${parameter=word}` as assignment 2020-03-16 23:04:54 +01:00
Joseph C. Sible 9d5363377e Simplify checkWhileReadPitfalls
* Clean up usage of not
* Use a case match instead of sequence_ and a do block
2020-03-16 00:14:22 -04:00
Joseph C. Sible 86d470c74f Simplify checkForInQuoted
* Avoid some unnecessary fmaps
* Reuse an identical pattern-match for two guards
* Apply De Morgan's law
* Use forM_ to avoid an unnecessary where
2020-03-15 16:05:55 -04:00
Vidar Holen a57f6d2886 Improve detection of for loops with single values 2020-03-15 11:30:56 -07:00
Vidar Holen c43b19f897 Make SC2095 (ssh in while read loops) more robust and suggest fixes 2020-03-14 21:15:47 -07:00
Austin English 741d499b3d src/ShellCheck/Analytics.hs: suggest using a shell directive for SC2148 2020-03-07 19:23:35 -06:00
Vidar Holen 7b998239af SC2257: Warn when changing arithmetic variables in redirections 2020-02-17 18:16:57 -08:00
Vidar Holen 4c9210af79 Inspect 'alias' commands for referenced variables (Fixes #1832) 2020-02-17 14:20:21 -08:00
Vidar Holen c2d67c15f8
Merge pull request #1802 from szydell/master
SC2016, repair false error for M language parser
2020-02-10 18:25:28 -08:00
Vidar Holen 6043deb8f2
Merge pull request #1824 from josephcsible/patch-1
Simplify literalEquals
2020-02-10 18:20:05 -08:00
Vidar Holen d0beac6d0b
Merge pull request #1826 from josephcsible/nofromjust
Use the Identity monad to avoid unnecessary uses of fromJust
2020-02-10 18:05:36 -08:00
Vidar Holen b88b253cad
Merge pull request #1827 from josephcsible/nofromjust2
Remove more unnecessary uses of fromJust
2020-02-10 18:01:38 -08:00
Joseph C. Sible 6d06103cab Remove unnecessary uses of head 2020-02-09 23:18:09 -05:00
Joseph C. Sible ea24e25efd Use Map.member instead of isJust and Map.lookup 2020-02-09 23:18:09 -05:00
Joseph C. Sible 8f0448133c Use isNothing instead of reimplementing it 2020-02-09 23:18:08 -05:00
Joseph C. Sible 7fc9496320 Use forM_ instead of reimplementing it 2020-02-09 23:18:08 -05:00
Joseph C. Sible 962fad038c Avoid a zip that breaks fusion 2020-02-09 23:18:08 -05:00
Joseph C. Sible a223a7a5a5 Remove unnecessary fromMaybes 2020-02-09 23:18:08 -05:00
Joseph C. Sible 8e9290badb Do toLower earlier 2020-02-09 23:17:53 -05:00
Joseph C. Sible 292b0840d9 Simplify a double negative 2020-02-09 23:17:53 -05:00
Joseph C. Sible 43c24cf79c Use Map.! instead of reimplementing it 2020-02-09 23:17:53 -05:00
Joseph C. Sible 21ad4196db Simplify findFunction 2020-02-09 23:17:53 -05:00
Joseph C. Sible 172aa7c4fc Avoid unnecessary use of when and unless 2020-02-09 23:17:53 -05:00
Joseph C. Sible c290eace54 Inline an uncurry 2020-02-09 23:17:53 -05:00
Joseph C. Sible a6efd02807 Simplify <> for SpaceStatus 2020-02-09 23:17:53 -05:00
Joseph C. Sible 057cc714b3 Simplify matchToken 2020-02-09 23:17:52 -05:00
Joseph C. Sible 0e00249eae Use void instead of do and return () 2020-02-09 23:17:52 -05:00
Joseph C. Sible 0ca50159ec Use head instead of reimplementing it
Normally I wouldn't use head, but this code is partial anyway.
2020-02-09 23:17:52 -05:00
Joseph C. Sible 7e6a556ef1 Get rid of potentially
This already exists as sequence_.
2020-02-09 23:17:52 -05:00
Joseph C. Sible 4bfe6496d9 Simplify check and checkTranslatedStringVariable
Avoid the "potentially" and "Maybe" business, and just use regular guards.
2020-02-09 23:17:52 -05:00
Joseph C. Sible ffbbfcfe25 Use mapM_ and sequence_ instead of reimplementing them 2020-02-09 23:17:52 -05:00
Joseph C. Sible cc424bac11 Use find instead of take 1 and filter 2020-02-09 23:17:52 -05:00
Joseph C. Sible cb01cbf7eb Use mapM instead of implementing a slower version of it 2020-02-09 23:17:52 -05:00
Joseph C. Sible 1e32139f66 Replace mapMaybe and concatMap with list comprehensions 2020-02-09 23:17:52 -05:00
Joseph C. Sible 4d92a2e15c Add getLiteralStringDef and simplify with it 2020-02-09 21:36:38 -05:00
Joseph C. Sible f8648e5465 Switch getLiteralStringExt to Identity where it can never be Nothing 2020-02-09 21:26:42 -05:00
Joseph C. Sible 4fd8de058b Remove more unnecessary uses of fromJust 2020-02-08 23:48:36 -05:00
Joseph C. Sible ef51ed3950
Simplify literalEquals 2020-02-08 14:09:17 -05:00
Vidar Holen 9d604ae732
Merge pull request #1822 from yetamrra/arrayindex
SC2191: Tighten index checks
2020-02-08 10:48:07 -08:00
Vidar Holen 1ca0b72329
Merge pull request #1816 from josephcsible/cleanups
Various cleanups and refactorings
2020-02-08 10:38:27 -08:00
Benjamin Gordon 474b23d6e7 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.
2020-02-05 16:50:32 -07:00
Joseph C. Sible e820a5642b Adjust a pattern to get rid of a fromJust 2020-02-02 00:40:22 -05:00
Joseph C. Sible 6595e14d25 Adjust a pattern to avoid tail 2020-02-02 00:24:24 -05:00
Joseph C. Sible 115ef29079 Use pattern matching instead of head 2020-02-02 00:16:59 -05:00
Joseph C. Sible c29b6afa56 Use null instead of comparing with empty lists 2020-02-01 23:04:04 -05:00
Joseph C. Sible f25b8bd03a Use gets instead of fmapping the result of get 2020-02-01 22:50:20 -05:00
Joseph C. Sible 28978a8b65 Use maybe instead of fromMaybe and fmap 2020-02-01 22:50:17 -05:00
Joseph C. Sible f5c6771016 Use find instead of listToMaybe and filter 2020-02-01 22:50:16 -05:00
Joseph C. Sible 3449e6be21 Get rid of our getOpt, as it already exists as lookup 2020-02-01 22:50:13 -05:00
Joseph C. Sible 2e52c2b56a Use notElem instead of not on the result of elem 2020-02-01 22:50:11 -05:00
Vidar Holen 1696296c0a Make SC2141 trigger more broadly 2020-02-01 16:51:40 -08:00
Peter Gromov a82e606e8d Don't trigger SC2154 (unassigned var) in `-n`/`-z` expressions #1583 2020-01-31 14:49:25 +01:00
Marcin Szydelski 93486ed6ac SC2016: disable for mumps -run %XCMD and LOOP%XCMD 2020-01-21 12:43:27 +01:00
Gandalf- fdd02c94c0 Issue 1759 mapfile and process substition
https://github.com/koalaman/shellcheck/issues/1759

When a simple process substition is used, this tripped up
the getMapfileArray function by making the last argument
not a variable
2019-12-22 23:19:03 -08:00
Vidar Holen cbcca528ae Merge branch 'iss1724-builtin-support' of https://github.com/jabberabbe/shellcheck into jabberabbe-iss1724-builtin-support 2019-12-21 18:13:07 -08:00
Vidar Holen 9f578f41a1 Explicitly add 'mappend' for old GHC versions 2019-11-16 11:16:15 -08:00
Vidar Holen f44624a9c0 Hide <> from Writer to not conflict with Semigroup 2019-11-14 20:02:25 -08:00
Vidar Holen c75bbcbd60 Include missing Semigroup import 2019-11-13 22:10:27 -08:00
Vidar Holen 4a63a3a8bd For SC2256, make sure the complete string is a variable name 2019-11-13 21:48:01 -08:00
Benjamin Gordon 2341a4c683 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.
2019-11-13 16:41:16 -07:00
Vidar Holen 5962b01816 Correctly handle empty variables for SC2086 (fixes #1722) 2019-11-03 12:46:25 -08:00
Tito Sacchi 0e0de94045 Fix issue #1724
(bash: missing support for 'builtin' keyword)
Now shellcheck looks for the arguments to 'builtin' to determine
read/written variables. A change in the parser makes sure that
assignments are parsed correctly in commands that start with 'builtin'.
2019-11-01 13:49:17 +01:00
Vidar Holen f042b0ebd1 Merge branch 'iboss-ptk-read-t-0' 2019-10-12 20:55:32 -07:00
Vidar Holen 764fdcb260 Move failing test to correct check 2019-10-12 20:50:55 -07:00
Vidar Holen 7473d4a743 Make `read -t 0` test more forgiving towards other flags 2019-10-12 20:45:36 -07:00
Vidar Holen 91abd979f2 Merge branch 'read-t-0' of https://github.com/iboss-ptk/shellcheck into iboss-ptk-read-t-0 2019-10-12 20:23:13 -07:00
Vidar Holen afea62de4e Suggest using `$((..))` in `[ 2*3 -eq 6 ]` (fixes #1641) 2019-10-12 19:55:20 -07:00
Supanat Pothivarakorn 7fb399528c Allow `read -t 0` to not require -r flag
since it has specific purpose for checking only
2019-10-02 22:34:43 +07:00
Vidar Holen de9ab4e6ef Fix glob range duplicate warning in [!!] (fixes #1706) 2019-09-28 14:03:11 -07:00
Vidar Holen e01c470598 Suggest quoting case patterns, as for SC2053 (fixes #1682) 2019-09-08 20:08:43 -07:00
Vidar Holen 788cf17076 Fix bad advice for SC2251 (fixes #1588) 2019-07-04 19:10:14 -07:00
Vidar Holen 78b8e76066 Also mention globbing in SC2206 (fixes #1626) 2019-07-04 17:43:18 -07:00
Vidar Holen 380221a02c Recognize `read -ra foo` as arrays (fixes #1636) 2019-07-03 20:40:41 -07:00
Vidar Holen be1f1c1ab7 Don't count 'readonly x' as a reference to x (fixes #1573) 2019-07-02 20:58:08 -07:00
Vidar Holen 3e3e4fd0cd Avoid defining flags for non-literal parameters 2019-07-01 23:22:09 -07:00
Vidar Holen 561075ea79 Merge branch 'shflags' of https://github.com/yetamrra/shellcheck into yetamrra-shflags 2019-07-01 20:00:28 -07:00
Vidar Holen eeb7ea01c9 Allow SC2103 to be silenced (fixes #1591) 2019-06-30 16:36:45 -07:00
Vidar Holen 3116ed3ae5 Filter warnings by annotations in unit tests 2019-06-30 16:36:03 -07:00
Benjamin Gordon f6ba500d6b Add support for basic shflags semantics
The shflags command-line flags library creates variables at runtime
with a few well-defined functions.  This causes shellcheck to spit out
lots of warnings about unassigned variables, as well as miss warnings
about unused flag variables.

We can address this with two parts:

1. Pretend that the shflags global variables are predefined like other
   shell variables so that shellcheck doesn't expect users to set them.
2. Treat DEFINE_string, DEFINE_int, etc. as new commands that create
   variables, similar to the existing read, local, mapfile, etc.

Part 1 can theoretically be addresssed without this by following sourced
files, but that doesn't help if people are otherwise not following
external sources.

The new behavior is on by default, similar to automatic bats test
behavior.

Addresses #1597
2019-06-25 12:14:54 -06:00
Vidar Holen 5242e384a1 Fix error spans for shebang warnings (fixes #1620) 2019-06-23 13:49:08 -07:00
Vidar Holen 7e77bfae49 Improve message for SC2055 2019-06-23 13:48:43 -07:00
Vidar Holen 9059024de6
Merge pull request #1592 from hugopeixoto/fix/SC2016-false-positive
SC2016: Don't trigger when using empty backticks
2019-06-19 19:55:34 -07:00
Vidar Holen 9f0ef5983a Optionally check for unassigned uppercase variables 2019-06-02 10:29:04 -07:00
Vidar Holen 3e7c2bfec0 Warn about [ $a != x ] || [ $a != y ] 2019-06-02 09:26:54 -07:00
Hugo Peixoto 07ffcb626e SC2016: Don't trigger when using empty backticks
When using '``' or '```', it should not suggest using double quotes.
2019-05-27 11:03:24 +01:00
Vidar Holen 95b1185882 Inform about ineffectual ! on commands (fixes #1531) 2019-05-22 17:14:28 -07:00
Vidar Holen 8efbecd64a Don't suggest removing braces from $((${x+1})) (fixes #1533) 2019-05-19 15:29:47 -07:00
Vidar Holen 52a9d90e1a
Merge pull request #1580 from virgilwashere/copyright
Update Copyright to year 2019; README linting
2019-05-19 12:03:15 -07:00
Vidar Holen 861b63aa77 Specify 'variable' in require-braces 2019-05-14 18:48:41 -07:00
Benjamin Gordon 64c9c83cc8 SC2250: New optional check for braces around variable references
Always using braces makes it harder to accidentally change a variable by
pasting other text next to it, but the warning is off by default because
it's definitely a style preference.  Omit special and positional
variables from the check because appending additional characters to them
already doesn't change parsing.
2019-05-14 11:01:38 -06:00
Benjamin Gordon aa3b709b5d Track whether braces were present in T_DollarBraced
References of the form $var and ${var} both map to the same structure in
the AST, which prevents any later analysis functions from distinguishing
them.  In preparation for adding checks that need this info, add a Bool
to T_DollarBraced that tracks whether the braces were seen at parsing
time and update all references so that this change is a no-op.
2019-05-14 11:01:38 -06:00
Virgil ea05271fa3 📝 Update Copyright to year 2019 and Markdown linting
- [x] 📝 Update Copyright to year 2019

- [x] 📝 MD009/no-trailing-spaces: Trailing spaces [Expected: 0 or 2; Actual: 1]
- [x] 📝 MD034/no-bare-urls: Bare URL used

- [ ] 📝 ~MD004/ul-style: Unordered list style [Expected: dash; Actual: asterisk]~
- [ ] ~add missing TOC entries~
2019-05-14 20:12:34 +10:00
Vidar Holen 50116e8aee Don't suggest [[..]] for sh in SC2081 (fixes #1562) 2019-05-13 20:45:53 -07:00
Vidar Holen 5fb1da6814 Replace verbose checks with optional checks 2019-05-12 19:14:04 -07:00
Vidar Holen a3cd5979a2 Update message for SC2171 2019-05-04 12:54:59 -07:00
Vidar Holen 37b24cc129 Don't warn about "a"b"c" in =~ regex (fixes #1565) 2019-05-04 12:18:45 -07:00
Vidar Holen 9470b9dc31 Don't mention arrays in SC2089 in sh/dash (fixes #1014) 2019-04-27 16:22:01 -07:00
Vidar Holen bf1003eae3 Auto-disable SC2119 when disabling SC2120 (fixes #703) 2019-04-27 15:20:07 -07:00
Vidar Holen b824294961 Limit SC2032 to likely command args (fixes #1537) 2019-04-14 20:58:01 -07:00
Vidar Holen 5b7354918f SC2249: When verbose, warn about missing default case (fixes #997) 2019-04-14 16:58:17 -07:00
Vidar Holen b76c0a8221 SC2248: Warn about unquoted variables without special chars 2019-04-13 20:19:13 -07:00
Vidar Holen c860b74505 Set SC2243/SC2244 level to "verbose" 2019-04-13 13:40:18 -07:00
Vidar Holen f514f5f735 Warn about flipped $ and " in $"(cmd)" (fixes #1517) 2019-03-20 22:10:04 -07:00
Tito Sacchi bd19ab4fa9 Fix issues #896 and #433: printf -v and arrays 2019-02-24 09:45:31 +01:00
Vidar Holen d4d219affd Don't warn that `cd ../..` and similar can fail in SC2164 2019-01-21 16:55:01 -08:00
Vidar Holen 489c3a4ddf Fix SC2164 always saying 'cd' even when using 'pushd' 2019-01-21 16:55:01 -08:00
Vidar Holen a621eba6d3
Merge pull request #1456 from contivero/issue-667
Silence SC2103 when using 'set -e' (fixes #667)
2019-01-20 14:54:29 -08:00
Vidar Holen f187382a0c Add bats support
This is motivated by the fact that the popularity of bats is increasing
since the creation of bats-core/bats-core.

The code is a cherry-pick of koalaman/shellcheck/bats branch.

Fix koalaman/shellcheck#417.
2019-01-20 14:59:37 +01:00
Vidar Holen e0a4241baa Warn if a shebang's interpreter ends in / (fixes #373) 2019-01-13 17:32:25 -08:00
Vidar Holen 1835ebd3a0 SC2245: Warn that Ksh [ -f * ] only applies to first (Fixes #1452) 2019-01-13 16:41:08 -08:00
Cristian Adrián Ontivero b34f4c1f4b
Silence SC2103 when using 'set -e' (fixes #667) 2019-01-13 16:20:41 +01:00
Vidar Holen e45b679d58
Merge pull request #1445 from Gandalf-/issue_1318_single_comma_array
Issue 1318 single comma array delimiter
2019-01-09 17:42:35 -08:00
Gandalf- 263401cfcb Issue 1318 single comma array delimiter
Issue https://github.com/koalaman/shellcheck/issues/1318

The case in which a single comma, with no spaces, used in an array
assignment is now caught for SC2054.
2019-01-08 19:56:34 -08:00
Vidar Holen 9c42d43e90 Merge branch 'sc2093-exec-in-loops' of https://github.com/jabberabbe/shellcheck 2019-01-08 18:31:59 -08:00
Vidar Holen 434b904746 Process replacements according to AST depth (fixes #1431) 2019-01-08 18:25:37 -08:00
Tito Sacchi ab2b0e11a3 Fix #1340 (SC2093 about removing "exec" should trigger in loops) 2019-01-08 20:20:26 +01:00
Gandalf- 6debd59f02 Add context to case pattern warnings
https://github.com/koalaman/shellcheck/issues/1039
2018-12-31 18:52:30 -08:00
Vidar Holen 73822c3588 Allow SC2243 and SC2244 to trigger with quotes, add fix 2018-12-28 19:02:06 -08:00