Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compilation warnings & housekeeping #35

Merged
merged 6 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
version: 2.0
version: 2.1
commands:
restore_mix_cache:
steps:
- restore_cache:
keys:
- mix-cache-{{ arch }}-{{ .Branch }}-{{ checksum "mix.lock" }}
- mix-cache-{{ arch }}-{{ .Branch }}
- mix-cache-{{ arch }}

jobs:
build:
test:
docker:
- image: circleci/elixir:1.9.4
- image: cimg/elixir:1.13
environment:
MIX_ENV: test

Expand All @@ -11,8 +20,43 @@ jobs:
steps:
- checkout
- run: mix local.hex --force
- restore_mix_cache
- run: mix deps.get
- run: mix format --check-formatted
- run: mix compile --force --warnings-as-errors
- save_cache:
key: mix-cache-{{ arch }}-{{ .Branch }}-{{ checksum "mix.lock" }}
paths: ["deps", "_build"]
- run: mix test
- run: MIX_ENV=dev mix docs && ! mix docs 2>&1 | grep -q "warning:"

lint:
docker:
- image: cimg/elixir:1.13
environment:
MIX_ENV: dev

working_directory: ~/app

steps:
- checkout
- run: mix local.hex --force
- restore_mix_cache
- run: mix deps.get
- run: mix format --check-formatted
- run: mix compile
- run: mix credo
- run: mix docs && mix docs 2>&1 | (! grep -q "warning:")
- run: elixir -v | tail -n 1 > .elixir_version
- restore_cache:
keys:
- dialyzer-cache-{{ arch }}-{{ checksum ".elixir_version" }}
- run: mix dialyzer
- save_cache:
key: dialyzer-cache-{{ arch }}-{{ checksum ".elixir_version" }}
paths: "priv/plts"

workflows:
version: 2
build:
jobs:
- test
- lint
187 changes: 187 additions & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# This file contains the configuration for Credo and you are probably reading
# this after creating it with `mix credo.gen.config`.
#
# If you find anything wrong or unclear in this file, please report an
# issue on GitHub: https://github.com/rrrene/credo/issues
#
%{
#
# You can have as many configs as you like in the `configs:` field.
configs: [
%{
#
# Run any config using `mix credo -C <name>`. If no config name is given
# "default" is used.
#
name: "default",
#
# These are the files included in the analysis:
files: %{
#
# You can give explicit globs or simply directories.
# In the latter case `**/*.{ex,exs}` will be used.
#
included: [
"lib/",
"src/",
"test/",
"web/",
"apps/*/lib/",
"apps/*/src/",
"apps/*/test/",
"apps/*/web/"
],
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
},
#
# Load and configure plugins here:
#
plugins: [],
#
# If you create your own checks, you must specify the source files for
# them here, so they can be loaded by Credo before running the analysis.
#
requires: [],
#
# If you want to enforce a style guide and need a more traditional linting
# experience, you can change `strict` to `true` below:
#
strict: false,
#
# To modify the timeout for parsing files, change this value:
#
parse_timeout: 5000,
#
# If you want to use uncolored output by default, you can change `color`
# to `false` below:
#
color: true,
#
# You can customize the parameters of any check by adding a second element
# to the tuple.
#
# To disable a check put `false` as second element:
#
# {Credo.Check.Design.DuplicatedCode, false}
#
checks: [
#
## Consistency Checks
#
{Credo.Check.Consistency.ExceptionNames, []},
{Credo.Check.Consistency.LineEndings, []},
{Credo.Check.Consistency.ParameterPatternMatching, []},
{Credo.Check.Consistency.SpaceAroundOperators, []},
{Credo.Check.Consistency.SpaceInParentheses, []},
{Credo.Check.Consistency.TabsOrSpaces, []},

#
## Design Checks
#
# You can customize the priority of any check
# Priority values are: `low, normal, high, higher`
#
{Credo.Check.Design.AliasUsage,
[priority: :low, if_nested_deeper_than: 2, if_called_more_often_than: 0]},
# You can also customize the exit_status of each check.
# If you don't want TODO comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
#
{Credo.Check.Design.TagTODO, [exit_status: 0]},
{Credo.Check.Design.TagFIXME, []},

#
## Readability Checks
#
{Credo.Check.Readability.AliasOrder, []},
{Credo.Check.Readability.FunctionNames, []},
{Credo.Check.Readability.LargeNumbers, []},
{Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]},
{Credo.Check.Readability.ModuleAttributeNames, []},
{Credo.Check.Readability.ModuleDoc, []},
{Credo.Check.Readability.ModuleNames, []},
{Credo.Check.Readability.ParenthesesInCondition, []},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, parens: true},
{Credo.Check.Readability.PredicateFunctionNames, []},
{Credo.Check.Readability.PreferImplicitTry, []},
{Credo.Check.Readability.RedundantBlankLines, []},
{Credo.Check.Readability.Semicolons, []},
{Credo.Check.Readability.SpaceAfterCommas, []},
{Credo.Check.Readability.StringSigils, []},
{Credo.Check.Readability.TrailingBlankLine, []},
{Credo.Check.Readability.TrailingWhiteSpace, []},
{Credo.Check.Readability.UnnecessaryAliasExpansion, []},
{Credo.Check.Readability.VariableNames, []},
{Credo.Check.Readability.WithSingleClause, false},

#
## Refactoring Opportunities
#
{Credo.Check.Refactor.CondStatements, []},
{Credo.Check.Refactor.CyclomaticComplexity, []},
{Credo.Check.Refactor.FunctionArity, []},
{Credo.Check.Refactor.LongQuoteBlocks, []},
{Credo.Check.Refactor.MapInto, false},
{Credo.Check.Refactor.MatchInCondition, []},
{Credo.Check.Refactor.NegatedConditionsInUnless, []},
{Credo.Check.Refactor.NegatedConditionsWithElse, []},
{Credo.Check.Refactor.Nesting, []},
{Credo.Check.Refactor.UnlessWithElse, []},
{Credo.Check.Refactor.WithClauses, []},

#
## Warnings
#
{Credo.Check.Warning.BoolOperationOnSameValues, []},
{Credo.Check.Warning.ExpensiveEmptyEnumCheck, []},
{Credo.Check.Warning.IExPry, []},
{Credo.Check.Warning.IoInspect, []},
{Credo.Check.Warning.LazyLogging, false},
{Credo.Check.Warning.MixEnv, []},
{Credo.Check.Warning.OperationOnSameValues, []},
{Credo.Check.Warning.OperationWithConstantResult, []},
{Credo.Check.Warning.RaiseInsideRescue, []},
{Credo.Check.Warning.UnusedEnumOperation, []},
{Credo.Check.Warning.UnusedFileOperation, []},
{Credo.Check.Warning.UnusedKeywordOperation, []},
{Credo.Check.Warning.UnusedListOperation, []},
{Credo.Check.Warning.UnusedPathOperation, []},
{Credo.Check.Warning.UnusedRegexOperation, []},
{Credo.Check.Warning.UnusedStringOperation, []},
{Credo.Check.Warning.UnusedTupleOperation, []},
{Credo.Check.Warning.UnsafeExec, []},

#
# Checks scheduled for next check update (opt-in for now, just replace `false` with `[]`)

#
# Controversial and experimental checks (opt-in, just replace `false` with `[]`)
#
{Credo.Check.Readability.StrictModuleLayout,
priority: :normal, order: ~w/shortdoc moduledoc behaviour use import require alias/a},
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},
{Credo.Check.Consistency.UnusedVariableNames, force: :meaningful},
{Credo.Check.Design.DuplicatedCode, false},
{Credo.Check.Readability.AliasAs, false},
{Credo.Check.Readability.MultiAlias, false},
{Credo.Check.Readability.Specs, []},
{Credo.Check.Readability.SinglePipe, false},
{Credo.Check.Readability.WithCustomTaggedTuple, false},
{Credo.Check.Refactor.ABCSize, false},
{Credo.Check.Refactor.AppendSingleItem, false},
{Credo.Check.Refactor.DoubleBooleanNegation, false},
{Credo.Check.Refactor.ModuleDependencies, false},
{Credo.Check.Refactor.NegatedIsNil, false},
{Credo.Check.Refactor.PipeChainStart, false},
{Credo.Check.Refactor.VariableRebinding, false},
{Credo.Check.Warning.LeakyEnvironment, false},
{Credo.Check.Warning.MapGetUnsafePass, false},
{Credo.Check.Warning.UnsafeToAtom, false}

#
# Custom checks can be created using `mix credo.gen.check`.
#
]
}
]
}
9 changes: 5 additions & 4 deletions lib/bunch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,7 @@ defmodule Bunch do
[nil]

"""
@spec listify(l) :: l when l: list
@spec listify(a) :: [a] when a: any
@spec listify(a | [a]) :: [a] when a: any
def listify(list) when is_list(list) do
list
end
Expand All @@ -297,7 +296,7 @@ defmodule Bunch do
@spec error_if_nil(value, reason) :: Type.try_t(value)
when value: any(), reason: any()
def error_if_nil(nil, reason), do: {:error, reason}
def error_if_nil(v, _), do: {:ok, v}
def error_if_nil(v, _reason), do: {:ok, v}

@doc """
Returns given stateful try value along with its status.
Expand Down Expand Up @@ -359,7 +358,7 @@ defmodule Bunch do
{:&, _meta, [i]} = node, acc when is_integer(i) ->
{node, acc}

{:&, meta, _}, _acc ->
{:&, meta, _args}, _acc ->
"""
The `&` (capture) operator is not allowed in lambda-like version of \
`#{inspect(__MODULE__)}.~>/2`. Use `&1` alone instead.
Expand Down Expand Up @@ -418,6 +417,8 @@ defmodule Bunch do
|> raise_compile_error(__CALLER__)
end

@spec raise_compile_error(term(), Macro.Env.t(), Keyword.t()) :: no_return()
@spec raise_compile_error(term(), Macro.Env.t()) :: no_return()
defp raise_compile_error(reason, caller, meta \\ []) do
raise CompileError,
file: caller.file,
Expand Down
4 changes: 3 additions & 1 deletion lib/bunch/access.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ defmodule Bunch.Access do
A bunch of functions for easier manipulation on terms of types implementing `Access`
behaviour.
"""
import Kernel, except: [get_in: 2, put_in: 2, update_in: 3, get_and_update_in: 3, pop_in: 2]

use Bunch

import Kernel, except: [get_in: 2, put_in: 2, update_in: 3, get_and_update_in: 3, pop_in: 2]

@compile {:inline, map_keys: 1}

@gen_common_docs fn fun_name ->
Expand Down
5 changes: 3 additions & 2 deletions lib/bunch/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ defmodule Bunch.Config do
A bunch of helpers for parsing and validating configurations.
"""

alias Bunch.Type
use Bunch

alias Bunch.Type

@doc """
Parses `config` according to `fields_specs`.

Expand Down Expand Up @@ -117,7 +118,7 @@ defmodule Bunch.Config do
end

defp parse_field(key, spec, {:ok, value}, config) do
validate = spec |> Map.get(:validate, fn _ -> :ok end)
validate = spec |> Map.get(:validate, fn _value -> :ok end)
in_enum = spec |> Map.get(:in, [value])

withl fun:
Expand Down
3 changes: 2 additions & 1 deletion lib/bunch/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Bunch.Macro do
defp replace_call(ast_node, {module, fun_name})
when is_atom(module) and is_atom(fun_name) do
case ast_node do
{^fun_name, _, args} ->
{^fun_name, _ctx, args} ->
quote do
apply(unquote(module), unquote(fun_name), unquote(args))
end
Expand Down Expand Up @@ -130,5 +130,6 @@ defmodule Bunch.Macro do
This function uses `Macro.expand/2` under the hood. Check
it out for more information and examples.
"""
@spec expand_deep(Macro.t(), Macro.Env.t()) :: Macro.t()
def expand_deep(ast, env), do: Macro.prewalk(ast, fn tree -> Macro.expand(tree, env) end)
end
2 changes: 1 addition & 1 deletion lib/bunch/short_ref.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Bunch.ShortRef do
@spec new(reference) :: t
def new(ref \\ make_ref()) do
ref_list = :erlang.ref_to_list(ref)
<<bin_hash_part::binary-size(4), _::binary>> = :crypto.hash(:sha, ref_list)
<<bin_hash_part::binary-size(4), _dropped::binary>> = :crypto.hash(:sha, ref_list)
hash = "#" <> Base.encode16(bin_hash_part, case: :lower)
%__MODULE__{ref: ref, hash: hash}
end
Expand Down
4 changes: 3 additions & 1 deletion lib/bunch/struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ defmodule Bunch.Struct do
@moduledoc """
A bunch of functions for easier manipulation on structs.
"""
import Kernel, except: [get_in: 2, put_in: 2, update_in: 3, get_and_update_in: 3, pop_in: 2]

use Bunch

import Kernel, except: [get_in: 2, put_in: 2, update_in: 3, get_and_update_in: 3, pop_in: 2]

@compile {:inline, map_keys: 1}

@gen_common_docs fn fun_name ->
Expand Down
3 changes: 2 additions & 1 deletion lib/bunch/typespec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ defmodule Bunch.Typespec do
:b

"""
defmacro @{:list_type, _, [{:"::", _, [{name, _, _} = name_var, list]}]} do
# credo:disable-for-next-line Credo.Check.Consistency.UnusedVariableNames
defmacro @{:list_type, _, [{:"::", _, [{name, _, _env} = name_var, list]}]} do
type = list |> Enum.reduce(fn a, b -> {:|, [], [a, b]} end)

quote do
Expand Down
Loading