From a123a3e6d0dfb83e409715dbf68d49e92b301f48 Mon Sep 17 00:00:00 2001 From: koalaman Date: Sun, 29 Oct 2017 16:49:18 -0700 Subject: [PATCH] Created SC1123 (markdown) --- SC1123.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 SC1123.md diff --git a/SC1123.md b/SC1123.md new file mode 100644 index 0000000..aa5d703 --- /dev/null +++ b/SC1123.md @@ -0,0 +1,58 @@ +## ShellCheck directives are only valid in front of complete compound commands, like `if`, not e.g. individual `elif` branches. + +### Problematic code: + +```sh +if [ "$prod" = "true" ] +then + echo "Prod mode" +# shellcheck disable=2154 +elif [ "$debug" = "true" ] +then + echo "Debug mode" +fi +``` + +### Correct code: +```sh +# Applies to entire `if...fi` command +# shellcheck disable=2154 +if [ "$prod" = "true" ] +then + echo "Prod mode" +elif [ "$debug" = "true" ] +then + echo "Debug mode" +fi +``` + +or + +```sh +if [ "$prod" = "true" ] +then + echo "Prod mode" +elif # Applies only to this [ .. ] command + # shellcheck disable=2154 + [ "$debug" = "true" ] +then + echo "Debug mode" +fi +``` + + +### Rationale: + +You appear to have put a directive before a non-command keyword, such as `elif`, `else`, `do`, `;;` or similar. + +Unlike many other linters, ShellCheck comment directives apply to the next shell command, rather than to the next line of text. + +This means that you can put a directive in front of a `while` loop, `if` statement or function definition, and it will apply to that entire structure. + +However, it also means that you can not apply the directive to non-commands like an individual `elif` or `else` block since these are not commands by themselves, and rather just parts of an `if` compound command. + +Please move the directive in front of the nearest applicable command that contains the code you want to apply it to, such as before the `if`. + +### Exceptions: + +None. \ No newline at end of file