|
21 | 21 |
|
22 | 22 | module Hint.NumLiteral (numLiteralHint) where |
23 | 23 |
|
| 24 | +import GHC.All (configuredExtensions) |
24 | 25 | import GHC.Hs |
25 | 26 | import GHC.Data.FastString |
26 | 27 | import GHC.LanguageExtensions.Type (Extension (..)) |
27 | 28 | import GHC.Types.SrcLoc |
28 | 29 | import GHC.Types.SourceText |
29 | 30 | import GHC.Util.ApiAnnotation (extensions) |
30 | 31 | import Data.Char (isDigit, isOctDigit, isHexDigit) |
| 32 | +import Data.Foldable (toList) |
31 | 33 | import Data.List (intercalate) |
32 | 34 | import Data.Set (union) |
33 | 35 | import Data.Generics.Uniplate.DataOnly (universeBi) |
34 | 36 | import Refact.Types |
35 | 37 |
|
36 | 38 | import Hint.Type (DeclHint, toSSA, modComments, firstDeclComments) |
37 | | -import Idea (Idea, suggest) |
| 39 | +import Idea (Idea(..), Note(..), suggest) |
38 | 40 |
|
39 | 41 | numLiteralHint :: DeclHint |
40 | 42 | numLiteralHint _ modu = |
41 | | - -- Comments appearing without an empty line before the first |
42 | | - -- declaration in a module are now associated with the declaration |
43 | | - -- not the module so to be safe, look also at `firstDeclComments |
44 | | - -- modu` (https://gitlab.haskell.org/ghc/ghc/-/merge_requests/9517). |
45 | | - let exts = union (extensions (modComments modu)) (extensions (firstDeclComments modu)) in |
46 | | - if NumericUnderscores `elem` exts then |
| 43 | + -- TODO: there's a subtle bug when the module disables `NumericUnderscores`. |
| 44 | + -- This seems pathological, though, because who would enable it for their |
| 45 | + -- project but disable it in specific files? |
| 46 | + if NumericUnderscores `elem` activeExtensions then |
47 | 47 | concatMap suggestUnderscore . universeBi |
48 | 48 | else |
49 | 49 | const [] |
| 50 | + where |
| 51 | + -- Comments appearing without an empty line before the first |
| 52 | + -- declaration in a module are now associated with the declaration |
| 53 | + -- not the module so to be safe, look also at `firstDeclComments |
| 54 | + -- modu` (https://gitlab.haskell.org/ghc/ghc/-/merge_requests/9517). |
| 55 | + moduleExtensions = extensions (modComments modu) `union` extensions (firstDeclComments modu) |
| 56 | + activeExtensions = configuredExtensions modu <> toList moduleExtensions |
50 | 57 |
|
51 | 58 | suggestUnderscore :: LHsExpr GhcPs -> [Idea] |
52 | 59 | suggestUnderscore x@(L _ (HsOverLit _ ol@(OverLit _ (HsIntegral intLit@(IL (SourceText srcTxt) _ _))))) = |
53 | | - [ suggest "Use underscore" (reLoc x) (reLoc y) [r] | '_' `notElem` unpackFS srcTxt, unpackFS srcTxt /= underscoredSrcTxt ] |
| 60 | + [ (suggest "Use underscore" (reLoc x) (reLoc y) [r]) |
| 61 | + { ideaNote = [ RequiresExtension "NumericUnderscores" ] |
| 62 | + } |
| 63 | + | '_' `notElem` unpackFS srcTxt, unpackFS srcTxt /= underscoredSrcTxt |
| 64 | + ] |
54 | 65 | where |
55 | 66 | underscoredSrcTxt = addUnderscore (unpackFS srcTxt) |
56 | 67 | y :: LocatedAn NoEpAnns (HsExpr GhcPs) |
57 | 68 | y = noLocA $ HsOverLit noExtField $ ol{ol_val = HsIntegral intLit{il_text = SourceText (fsLit underscoredSrcTxt)}} |
58 | 69 | r = Replace Expr (toSSA x) [("a", toSSA y)] "a" |
59 | 70 | suggestUnderscore x@(L _ (HsOverLit _ ol@(OverLit _ (HsFractional fracLit@(FL (SourceText srcTxt) _ _ _ _))))) = |
60 | | - [ suggest "Use underscore" (reLoc x) (reLoc y) [r] | '_' `notElem` unpackFS srcTxt, unpackFS srcTxt /= underscoredSrcTxt ] |
| 71 | + [ (suggest "Use underscore" (reLoc x) (reLoc y) [r]) |
| 72 | + { ideaNote = [ RequiresExtension "NumericUnderscores" ] |
| 73 | + } |
| 74 | + | '_' `notElem` unpackFS srcTxt, unpackFS srcTxt /= underscoredSrcTxt |
| 75 | + ] |
61 | 76 | where |
62 | 77 | underscoredSrcTxt = addUnderscore (unpackFS srcTxt) |
63 | 78 | y :: LocatedAn NoEpAnns (HsExpr GhcPs) |
|
0 commit comments