From d6bb8fc0d8aa40e72b2b894b16d7eacdafcf94a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20R=C3=B6hrich?= Date: Sun, 14 Feb 2021 19:13:29 +0100 Subject: [PATCH 1/2] Error on backslash in comment #2132 - Report error in case of a backspace in a comment Backspaces in comments are no good. In most cases they are the result of commenting out a longer line, that was broken down. This usually results in the shell treating the following lines as their own commands on their own lines instead of as parts of the longer, broken down line. --- src/ShellCheck/Parser.hs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ShellCheck/Parser.hs b/src/ShellCheck/Parser.hs index 03865af..a999b23 100644 --- a/src/ShellCheck/Parser.hs +++ b/src/ShellCheck/Parser.hs @@ -1039,9 +1039,14 @@ readComment = do unexpecting "shellcheck annotation" readAnnotationPrefix readAnyComment +prop_readAnyComment = isOk readAnyComment "# Comment" +prop_readAnyComment1 = not $ isOk readAnyComment "# Comment \\\n" readAnyComment = do char '#' - many $ noneOf "\r\n" + comment <- many $ noneOf "\\\r\n" + bs <- many $ oneOf "\\" + unless (null bs) (fail "Backslash in or directly after comment") + return comment prop_readNormalWord = isOk readNormalWord "'foo'\"bar\"{1..3}baz$(lol)" prop_readNormalWord2 = isOk readNormalWord "foo**(foo)!!!(@@(bar))" From 98952df35b834315c50c2ce6fe8f96516bf2a7ae Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 20 Mar 2021 13:58:37 -0700 Subject: [PATCH 2/2] Improve warnings on backslashes in comments --- CHANGELOG.md | 1 + src/ShellCheck/Parser.hs | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d7a1ac..9eec957 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Git ### Added - `disable` directives can now be a range, e.g. `disable=SC3000-SC4000` +- SC1143: Warn about line continuations in comments - SC2259/SC2260: Warn when redirections override pipes - SC2261: Warn about multiple competing redirections - SC2262/SC2263: Warn about aliases declared and used in the same parsing unit diff --git a/src/ShellCheck/Parser.hs b/src/ShellCheck/Parser.hs index 8db1991..3c16d5d 100644 --- a/src/ShellCheck/Parser.hs +++ b/src/ShellCheck/Parser.hs @@ -87,11 +87,23 @@ extglobStart = oneOf extglobStartChars unicodeDoubleQuotes = "\x201C\x201D\x2033\x2036" unicodeSingleQuotes = "\x2018\x2019" -prop_spacing = isOk spacing " \\\n # Comment" +prop_spacing1 = isOk spacing " \\\n # Comment" +prop_spacing2 = isOk spacing "# We can continue lines with \\" +prop_spacing3 = isWarning spacing " \\\n # --verbose=true \\" spacing = do - x <- many (many1 linewhitespace <|> try (string "\\\n" >> return "")) + x <- many (many1 linewhitespace <|> continuation) optional readComment return $ concat x + where + continuation = do + try (string "\\\n") + -- The line was continued. Warn if this next line is a comment with a trailing \ + whitespace <- many linewhitespace + optional $ do + x <- readComment + when ("\\" `isSuffixOf` x) $ + parseProblem ErrorC 1143 "This backslash is part of a comment and does not continue the line." + return whitespace spacing1 = do spacing <- spacing @@ -1040,13 +1052,9 @@ readComment = do readAnyComment prop_readAnyComment = isOk readAnyComment "# Comment" -prop_readAnyComment1 = not $ isOk readAnyComment "# Comment \\\n" readAnyComment = do char '#' - comment <- many $ noneOf "\\\r\n" - bs <- many $ oneOf "\\" - unless (null bs) (fail "Backslash in or directly after comment") - return comment + many $ noneOf "\r\n" prop_readNormalWord = isOk readNormalWord "'foo'\"bar\"{1..3}baz$(lol)" prop_readNormalWord2 = isOk readNormalWord "foo**(foo)!!!(@@(bar))" @@ -1409,6 +1417,8 @@ readNormalEscaped = called "escaped char" $ do do next <- quotable <|> oneOf "?*@!+[]{}.,~#" when (next == ' ') $ checkTrailingSpaces pos <|> return () + -- Check if this line is followed by a commented line with a trailing backslash + when (next == '\n') $ try . lookAhead $ void spacing return $ if next == '\n' then "" else [next] <|> do