Skip to content

Commit 2a6d7d3

Browse files
local
1 parent 6101eb1 commit 2a6d7d3

File tree

1 file changed

+59
-25
lines changed

1 file changed

+59
-25
lines changed

sheriff/src/Sheriff/Plugin.hs

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -480,15 +480,12 @@ extractFunctionNameFromContext expr = case unLoc expr of
480480
isBadExpr :: (HasPluginOpts PluginOpts) => Rules -> LHsExpr GhcTc -> TcM [(LHsExpr GhcTc, Violation)]
481481
isBadExpr rules ap@(L _ (HsVar _ v)) = do
482482
let fnNames = extractFunctionName ap
483-
liftIO $ putStrLn $ "Detected HsVar function name: " <> fromMaybe "Unknown_HsVar" fnNames
484483
isBadExprHelper rules ap (fromMaybe "Unknown_HsVar" fnNames)
485484
isBadExpr rules ap@(L _ (HsApp _ funl funr)) = do
486485
let fnNames = extractFunctionName funl
487-
liftIO $ putStrLn $ "Detected HsApp function name: " <> fromMaybe "Unknown_HsApp" fnNames
488486
isBadExprHelper rules ap (fromMaybe "Unknown_HsApp" fnNames)
489487
isBadExpr rules ap@(L _ (PatExplicitList _ _)) = do
490488
let fnNames = extractFunctionNameFromContext ap
491-
liftIO $ putStrLn $ "Detected PatExplicitList function name: " <> fromMaybe "Unknown_PatExplicitList" fnNames
492489
isBadExprHelper rules ap (fromMaybe "Unknown_PatExplicitList" fnNames)
493490
isBadExpr rules ap@(L loc (PatHsWrap _ expr)) = isBadExpr rules (L loc expr) >>= mapM (\(x, y) -> trfViolationErrorInfo y ap x >>= \z -> pure (x, z))
494491
isBadExpr rules ap@(L loc (OpApp _ lfun op rfun)) = do
@@ -1065,31 +1062,68 @@ getFnNameAndTypeableExprWithAllArgs _ = Nothing
10651062

10661063
-- TODO: Verify the correctness of this function before moving it to utils
10671064
-- Get function name with all it's arguments
1065+
10681066
getFnNameWithAllArgs :: LHsExpr GhcTc -> Maybe (Located Var, [LHsExpr GhcTc])
1069-
getFnNameWithAllArgs (L loc (HsVar _ v)) = Just (getLocated v loc, [])
1070-
getFnNameWithAllArgs (L _ (HsConLikeOut _ cl)) = (\clId -> (noExprLoc clId, [])) <$> conLikeWrapId cl
1071-
getFnNameWithAllArgs (L _ (HsAppType _ expr _)) = getFnNameWithAllArgs expr
1072-
getFnNameWithAllArgs (L _ (HsApp _ (L loc (HsVar _ v)) funr)) = Just (getLocated v loc, [funr])
1073-
getFnNameWithAllArgs (L _ (HsPar _ expr)) = getFnNameWithAllArgs expr
1074-
getFnNameWithAllArgs (L _ (HsApp _ funl funr)) = do
1075-
let res = getFnNameWithAllArgs funl
1076-
case res of
1077-
Nothing -> Nothing
1078-
Just (fnName, ls) -> Just (fnName, ls ++ [funr])
1079-
getFnNameWithAllArgs (L loc (OpApp _ funl op funr)) = do
1080-
case showS op of
1081-
"($)" -> getFnNameWithAllArgs $ (L loc (HsApp noExtFieldOrAnn funl funr))
1082-
_ -> Nothing
1083-
getFnNameWithAllArgs (L loc ap@(PatHsWrap _ expr)) = getFnNameWithAllArgs (L loc expr)
1067+
getFnNameWithAllArgs expr = case expr of
1068+
L loc (HsVar _ v) ->
1069+
trace ("getFnNameWithAllArgs: Detected HsVar with name = " <> showS v) $
1070+
Just (getLocated v loc, [])
1071+
1072+
L _ (HsConLikeOut _ cl) ->
1073+
trace "getFnNameWithAllArgs: Detected HsConLikeOut" $
1074+
(\clId -> (noExprLoc clId, [])) <$> conLikeWrapId cl
1075+
1076+
L _ (HsAppType _ expr _) ->
1077+
trace "getFnNameWithAllArgs: Detected HsAppType" $
1078+
getFnNameWithAllArgs expr
1079+
1080+
L _ (HsApp _ (L loc (HsVar _ v)) funr) ->
1081+
trace ("getFnNameWithAllArgs: Detected HsApp with HsVar function name = " <> showS v) $
1082+
Just (getLocated v loc, [funr])
1083+
1084+
L _ (HsPar _ expr) ->
1085+
trace "getFnNameWithAllArgs: Detected HsPar" $
1086+
getFnNameWithAllArgs expr
1087+
1088+
L _ (HsApp _ funl funr) ->
1089+
trace "getFnNameWithAllArgs: Detected HsApp with nested function application" $
1090+
case getFnNameWithAllArgs funl of
1091+
Nothing -> trace "getFnNameWithAllArgs: No function name found in nested HsApp" Nothing
1092+
Just (fnName, ls) ->
1093+
trace ("getFnNameWithAllArgs: Found function name = " <> showS fnName <> " with arguments = " <> showS ls) $
1094+
Just (fnName, ls ++ [funr])
1095+
1096+
L loc (OpApp _ funl op funr) ->
1097+
trace ("getFnNameWithAllArgs: Detected OpApp with operator = " <> showS op) $
1098+
case showS op of
1099+
"($)" ->
1100+
trace "getFnNameWithAllArgs: Detected ($) operator, treating as HsApp" $
1101+
getFnNameWithAllArgs (L loc (HsApp noExtFieldOrAnn funl funr))
1102+
_ -> trace "getFnNameWithAllArgs: Unsupported operator, returning Nothing" Nothing
1103+
1104+
L loc ap@(PatHsWrap _ expr) ->
1105+
trace "getFnNameWithAllArgs: Detected PatHsWrap" $
1106+
getFnNameWithAllArgs (L loc expr)
1107+
10841108
#if __GLASGOW_HASKELL__ >= 900
1085-
getFnNameWithAllArgs (L loc ap@(PatHsExpansion orig expanded)) =
1086-
case (orig, expanded) of
1087-
((OpApp _ _ op _), (HsApp _ (L _ (HsApp _ op' funl)) funr)) -> case showS op of
1088-
"($)" -> getFnNameWithAllArgs (L loc (HsApp noExtFieldOrAnn funl funr))
1089-
_ -> getFnNameWithAllArgs (L loc expanded)
1090-
_ -> getFnNameWithAllArgs (L loc expanded)
1109+
L loc ap@(PatHsExpansion orig expanded) ->
1110+
trace "getFnNameWithAllArgs: Detected PatHsExpansion" $
1111+
case (orig, expanded) of
1112+
((OpApp _ _ op _), (HsApp _ (L _ (HsApp _ op' funl)) funr)) ->
1113+
case showS op of
1114+
"($)" ->
1115+
trace "getFnNameWithAllArgs: Detected ($) operator in PatHsExpansion, treating as HsApp" $
1116+
getFnNameWithAllArgs (L loc (HsApp noExtFieldOrAnn funl funr))
1117+
_ ->
1118+
trace "getFnNameWithAllArgs: Unsupported operator in PatHsExpansion, processing expanded expression" $
1119+
getFnNameWithAllArgs (L loc expanded)
1120+
_ ->
1121+
trace "getFnNameWithAllArgs: Processing expanded expression in PatHsExpansion" $
1122+
getFnNameWithAllArgs (L loc expanded)
10911123
#endif
1092-
getFnNameWithAllArgs _ = Nothing
1124+
1125+
_ ->
1126+
trace "getFnNameWithAllArgs: No matching case found, returning Nothing" Nothing
10931127

10941128
--------------------------- Sheriff Plugin Utils ---------------------------
10951129
-- Transform the FnBlockedInArg Violation with correct expression

0 commit comments

Comments
 (0)