Skip to content

Commit

Permalink
[#372] Warn on usages ofreadFileText and writeFileText (#407)
Browse files Browse the repository at this point in the history
Resolves #372
  • Loading branch information
chshersh committed Apr 14, 2022
1 parent fcb3225 commit 1cd9f2e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The changelog is available [on GitHub][2].
## Unreleased

* Remove Option from Data.Semigroup (which was removed from base in base 4.16)
* [#372](https://github.com/kowainik/relude/issues/372):
Warn on usages of `readFileText`, `readFileLText`, `readFile` and `readFile'`.
* Support `hashable ^>=1.4`

## 1.0.0.1 — Mar 15, 2021
Expand Down
11 changes: 11 additions & 0 deletions src/Relude/File.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ also faster since they don't check encoding). However, you can then decode that
data with the help of functions from the "Relude.String.Conversion" module, e. g.
'Relude.String.Conversion.decodeUtf8'.
To be more precise, avoid the following functions:
* 'readFileText'
* 'readFileLText'
See the following blog post for more details:
* [Beware of readFile by Michael Snoyman](https://www.snoyman.com/blog/2016/12/beware-of-readfile/)
@since 0.3.0
-}

Expand Down Expand Up @@ -68,6 +77,7 @@ readFileText :: MonadIO m => FilePath -> m Text
readFileText = liftIO . T.readFile
{-# SPECIALIZE readFileText :: FilePath -> IO Text #-}
{-# INLINE readFileText #-}
{-# WARNING readFileText ["'readFileText' depends on the system's locale settings and can throw unexpected exceptions.", "Use 'readFileBS' instead."] #-}

{- | Lifted version of 'T.writeFile'.
Expand Down Expand Up @@ -99,6 +109,7 @@ readFileLText :: MonadIO m => FilePath -> m LText
readFileLText = liftIO . LT.readFile
{-# SPECIALIZE readFileLText :: FilePath -> IO LText #-}
{-# INLINE readFileLText #-}
{-# WARNING readFileLText ["'readFileLText' depends on the system's locale settings and can throw unexpected exceptions.", "Use 'readFileLBS' instead."] #-}

{- | Lifted version of 'LT.writeFile'.
Expand Down
2 changes: 2 additions & 0 deletions src/Relude/Lifted/File.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ readFile :: MonadIO m => FilePath -> m String
readFile = liftIO . IO.readFile
{-# SPECIALIZE readFile :: FilePath -> IO String #-}
{-# INLINE readFile #-}
{-# WARNING readFile ["'readFile' depends on the system's locale settings and can throw unexpected exceptions.", "Use 'readFileBS' or 'readFileLBS' instead."] #-}

#if ( __GLASGOW_HASKELL__ >= 900 )
{- | Lifted version of 'IO.readFile''. Strict version of 'readFile'.
Expand All @@ -45,6 +46,7 @@ readFile' :: MonadIO m => FilePath -> m String
readFile' = liftIO . IO.readFile'
{-# SPECIALIZE readFile' :: FilePath -> IO String #-}
{-# INLINE readFile' #-}
{-# WARNING readFile' ["readFile' depends on the system's locale settings and can throw unexpected exceptions.", "Use 'readFileBS' or 'readFileLBS' instead."] #-}
#endif

-- | Lifted version of 'IO.writeFile'.
Expand Down

0 comments on commit 1cd9f2e

Please sign in to comment.