From a2dde5f7a161499632236cd3d5095b23439f4c13 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sat, 2 Apr 2022 21:13:03 +0000 Subject: [PATCH] feat: Require that `*_free` functions are nullable. These should always be no-ops when passed a null pointer. --- src/Tokstyle/Linter/NonNull.hs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Tokstyle/Linter/NonNull.hs b/src/Tokstyle/Linter/NonNull.hs index 58b51b6..d794411 100644 --- a/src/Tokstyle/Linter/NonNull.hs +++ b/src/Tokstyle/Linter/NonNull.hs @@ -59,6 +59,12 @@ checkParams file (indices -> nonnull) (indices -> nullable) params = do p:_ -> "`" <> showNode p <> "`" +isDestructor :: Lexeme Text -> Bool +isDestructor name = + "_free" `Text.isSuffixOf` lexemeText name || + "_kill" `Text.isSuffixOf` lexemeText name + + linter :: AstActions (State Linter) Text linter = astActions { doNode = \file node act -> @@ -66,6 +72,9 @@ linter = astActions FunctionDecl Static (Fix (FunctionPrototype _ name _)) -> State.modify $ \l@Linter{statics} -> l{statics = (lexemeText name, name) : statics} + NonNull _ [] (Fix (FunctionDecl _ (Fix (FunctionPrototype _ name [_])))) | isDestructor name -> + warn file name "destructor function (free/kill) must accept nullable arguments" + NonNull nonnull nullable (Fix (FunctionDefn Static (Fix (FunctionPrototype _ name params)) _)) -> do checkParams file nonnull nullable params Linter{statics} <- State.get