-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1074 from frerich/detect_compositions_of_utc_now_…
…and_truncate Detect compositions of {Naive}DateTime.utc_now and {Naive}DateTime.truncate
- Loading branch information
Showing
3 changed files
with
508 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
defmodule Credo.Check.Refactor.UtcNowTruncate do | ||
use Credo.Check, | ||
id: "EX4032", | ||
base_priority: :high, | ||
explanations: [ | ||
check: """ | ||
`DateTime.utc_now/1` is more efficient than `DateTime.utc_now/0 |> DateTime.truncate/1`. | ||
For example, the code here ... | ||
DateTime.utc_now() |> DateTime.truncate(:second) | ||
NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) | ||
... can be refactored to look like this: | ||
DateTime.utc_now(:second) | ||
NaiveDateTime.utc_now(:second) | ||
The reason for this is not just performance, because no separate function | ||
call is required, but also brevity of the resulting code. | ||
""" | ||
] | ||
|
||
@doc false | ||
def run(source_file, params \\ []) do | ||
issue_meta = IssueMeta.for(source_file, params) | ||
|
||
Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta)) | ||
end | ||
|
||
# DateTime.truncate(DateTime.utc_now(), _) | ||
# DateTime.truncate(DateTime.utc_now(_), _) | ||
# DateTime.truncate(DateTime.utc_now(_, _), _) | ||
defp traverse( | ||
{{:., meta, [{:__aliases__, _, [:DateTime]}, :truncate]}, _, | ||
[ | ||
{{:., _, [{:__aliases__, _, [:DateTime]}, :utc_now]}, _, _}, | ||
_ | ||
]} = | ||
ast, | ||
issues, | ||
issue_meta | ||
) do | ||
new_issue = issue_for(issue_meta, meta[:line], "DateTime") | ||
{ast, issues ++ List.wrap(new_issue)} | ||
end | ||
|
||
# DateTime.utc_now() |> DateTime.truncate(_) | ||
# DateTime.utc_now(_) |> DateTime.truncate(_) | ||
# DateTime.utc_now(_, _) |> DateTime.truncate(_) | ||
defp traverse( | ||
{:|>, _, | ||
[ | ||
{{:., _, [{:__aliases__, _, [:DateTime]}, :utc_now]}, _, _}, | ||
{{:., meta, [{:__aliases__, _, [:DateTime]}, :truncate]}, _, [_]} | ||
]} = ast, | ||
issues, | ||
issue_meta | ||
) do | ||
new_issue = issue_for(issue_meta, meta[:line], "DateTime") | ||
{ast, issues ++ List.wrap(new_issue)} | ||
end | ||
|
||
# DateTime.truncate(_ |> DateTime.utc_now(), _) | ||
# DateTime.truncate(_ |> DateTime.utc_now(_), _) | ||
defp traverse( | ||
{{:., meta, [{:__aliases__, _, [:DateTime]}, :truncate]}, _, | ||
[ | ||
{:|>, _, | ||
[ | ||
_, | ||
{{:., _, [{:__aliases__, _, [:DateTime]}, :utc_now]}, _, _} | ||
]}, | ||
_ | ||
]} = ast, | ||
issues, | ||
issue_meta | ||
) do | ||
new_issue = issue_for(issue_meta, meta[:line], "DateTime") | ||
{ast, issues ++ List.wrap(new_issue)} | ||
end | ||
|
||
# _ |> DateTime.utc_now() |> DateTime.truncate(_) | ||
# _ |> DateTime.utc_now(_) |> DateTime.truncate(_) | ||
defp traverse( | ||
{:|>, _, | ||
[ | ||
{:|>, _, | ||
[ | ||
_, | ||
{{:., _, [{:__aliases__, _, [:DateTime]}, :utc_now]}, _, _} | ||
]}, | ||
{{:., meta, [{:__aliases__, _, [:DateTime]}, :truncate]}, _, [_]} | ||
]} = ast, | ||
issues, | ||
issue_meta | ||
) do | ||
new_issue = issue_for(issue_meta, meta[:line], "DateTime") | ||
{ast, issues ++ List.wrap(new_issue)} | ||
end | ||
|
||
# NaiveDateTime.truncate(NaiveDateTime.utc_now(), _) | ||
# NaiveDateTime.truncate(NaiveDateTime.utc_now(_), _) | ||
# NaiveDateTime.truncate(NaiveDateTime.utc_now(_, _), _) | ||
defp traverse( | ||
{{:., meta, [{:__aliases__, _, [:NaiveDateTime]}, :truncate]}, _, | ||
[ | ||
{{:., _, [{:__aliases__, _, [:NaiveDateTime]}, :utc_now]}, _, _}, | ||
_ | ||
]} = | ||
ast, | ||
issues, | ||
issue_meta | ||
) do | ||
new_issue = issue_for(issue_meta, meta[:line], "NaiveDateTime") | ||
{ast, issues ++ List.wrap(new_issue)} | ||
end | ||
|
||
# NaiveDateTime.utc_now() |> NaiveDateTime.truncate(_) | ||
# NaiveDateTime.utc_now(_) |> NaiveDateTime.truncate(_) | ||
# NaiveDateTime.utc_now(_, _) |> NaiveDateTime.truncate(_) | ||
defp traverse( | ||
{:|>, _, | ||
[ | ||
{{:., _, [{:__aliases__, _, [:NaiveDateTime]}, :utc_now]}, _, _}, | ||
{{:., meta, [{:__aliases__, _, [:NaiveDateTime]}, :truncate]}, _, [_]} | ||
]} = ast, | ||
issues, | ||
issue_meta | ||
) do | ||
new_issue = issue_for(issue_meta, meta[:line], "NaiveDateTime") | ||
{ast, issues ++ List.wrap(new_issue)} | ||
end | ||
|
||
# NaiveDateTime.truncate(_ |> NaiveDateTime.utc_now(), _) | ||
# NaiveDateTime.truncate(_ |> NaiveDateTime.utc_now(_), _) | ||
defp traverse( | ||
{{:., meta, [{:__aliases__, _, [:NaiveDateTime]}, :truncate]}, _, | ||
[ | ||
{:|>, _, | ||
[ | ||
_, | ||
{{:., _, [{:__aliases__, _, [:NaiveDateTime]}, :utc_now]}, _, _} | ||
]}, | ||
_ | ||
]} = ast, | ||
issues, | ||
issue_meta | ||
) do | ||
new_issue = issue_for(issue_meta, meta[:line], "NaiveDateTime") | ||
{ast, issues ++ List.wrap(new_issue)} | ||
end | ||
|
||
# _ |> NaiveDateTime.utc_now() |> NaiveDateTime.truncate(_) | ||
# _ |> NaiveDateTime.utc_now(_) |> NaiveDateTime.truncate(_) | ||
defp traverse( | ||
{:|>, _, | ||
[ | ||
{:|>, _, | ||
[ | ||
_, | ||
{{:., _, [{:__aliases__, _, [:NaiveDateTime]}, :utc_now]}, _, _} | ||
]}, | ||
{{:., meta, [{:__aliases__, _, [:NaiveDateTime]}, :truncate]}, _, [_]} | ||
]} = ast, | ||
issues, | ||
issue_meta | ||
) do | ||
new_issue = issue_for(issue_meta, meta[:line], "NaiveDateTime") | ||
{ast, issues ++ List.wrap(new_issue)} | ||
end | ||
|
||
defp traverse(ast, issues, _issue_meta) do | ||
{ast, issues} | ||
end | ||
|
||
defp issue_for(issue_meta, line_no, module) do | ||
format_issue( | ||
issue_meta, | ||
message: | ||
"Pass time unit to `#{module}.utc_now` instead of composing with `#{module}.truncate/2`.", | ||
trigger: "#{module}.truncate", | ||
line_no: line_no | ||
) | ||
end | ||
end |
Oops, something went wrong.