Skip to content

Commit

Permalink
feat: Require that *_free functions are nullable.
Browse files Browse the repository at this point in the history
These should always be no-ops when passed a null pointer.
  • Loading branch information
iphydf committed Apr 2, 2022
1 parent 391d878 commit a2dde5f
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Tokstyle/Linter/NonNull.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,22 @@ 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 ->
case unFix node of
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
Expand Down

0 comments on commit a2dde5f

Please sign in to comment.