Skip to content

Commit

Permalink
Clean up Credo/Dialyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuawscott committed May 5, 2018
1 parent 861d234 commit 2d352cb
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 18 deletions.
159 changes: 159 additions & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# 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 exec using `mix credo -C <name>`. If no exec 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/"],
excluded: [~r"/_build/", ~r"/deps/"]
},
#
# 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,
#
# 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},
# For some checks, you can also set other parameters
#
# If you don't want the `setup` and `test` macro calls in ExUnit tests
# or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
#
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},
# 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: 2},
{Credo.Check.Design.TagFIXME},

#
## Readability Checks
#
{Credo.Check.Readability.FunctionNames},
{Credo.Check.Readability.LargeNumbers},
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 100},
{Credo.Check.Readability.ModuleAttributeNames},
{Credo.Check.Readability.ModuleDoc},
{Credo.Check.Readability.ModuleNames},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs},
{Credo.Check.Readability.ParenthesesInCondition},
{Credo.Check.Readability.PredicateFunctionNames},
{Credo.Check.Readability.PreferImplicitTry},
{Credo.Check.Readability.RedundantBlankLines},
{Credo.Check.Readability.StringSigils},
{Credo.Check.Readability.TrailingBlankLine},
{Credo.Check.Readability.TrailingWhiteSpace},
{Credo.Check.Readability.VariableNames},
{Credo.Check.Readability.Semicolons},
{Credo.Check.Readability.SpaceAfterCommas},

#
## Refactoring Opportunities
#
{Credo.Check.Refactor.DoubleBooleanNegation},
{Credo.Check.Refactor.CondStatements},
{Credo.Check.Refactor.CyclomaticComplexity},
{Credo.Check.Refactor.FunctionArity},
{Credo.Check.Refactor.LongQuoteBlocks},
{Credo.Check.Refactor.MatchInCondition},
{Credo.Check.Refactor.NegatedConditionsInUnless},
{Credo.Check.Refactor.NegatedConditionsWithElse},
{Credo.Check.Refactor.Nesting},
{Credo.Check.Refactor.PipeChainStart,
excluded_argument_types: [:atom, :binary, :fn, :keyword], excluded_functions: []},
{Credo.Check.Refactor.UnlessWithElse},

#
## Warnings
#
{Credo.Check.Warning.BoolOperationOnSameValues},
{Credo.Check.Warning.ExpensiveEmptyEnumCheck},
{Credo.Check.Warning.IExPry},
{Credo.Check.Warning.IoInspect},
{Credo.Check.Warning.LazyLogging},
{Credo.Check.Warning.OperationOnSameValues},
{Credo.Check.Warning.OperationWithConstantResult},
{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.RaiseInsideRescue},

#
# Controversial and experimental checks (opt-in, just remove `, false`)
#
{Credo.Check.Refactor.ABCSize, false},
{Credo.Check.Refactor.AppendSingleItem, false},
{Credo.Check.Refactor.VariableRebinding, false},
{Credo.Check.Warning.MapGetUnsafePass, false},
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},

#
# Deprecated checks (these will be deleted after a grace period)
#
{Credo.Check.Readability.Specs, false}

#
# Custom checks can be created using `mix credo.gen.check`.
#
]
}
]
}
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ elixir:
- '1.6.3'
before_script:
sudo redis-server --dir $TRAVIS_BUILD_DIR --dbfilename dump.rdb &
script:
- mix test
- mix credo --strict
- mix dialyzer
4 changes: 2 additions & 2 deletions lib/rdb_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ defmodule RdbParser do
"""
@spec stream_entries(binary, stream_options) :: Enumerable.t()
def stream_entries(filename, opts \\ []) do
chunk_size = Keyword.get(opts, :chunk_size, 65536)
chunk_size = Keyword.get(opts, :chunk_size, 65_536)

filename
|> File.stream!([], chunk_size)
Expand Down Expand Up @@ -282,8 +282,8 @@ defmodule RdbParser do

def parse(<<unsupported_type::size(8), rest::binary>>, _entries)
when unsupported_type <= 15 do
IO.inspect(rest)
Logger.warn("unsupported key type #{unsupported_type}")
{:parse_error, rest}
end

# Fallback case - this should mean that we don't have the right length in
Expand Down
9 changes: 6 additions & 3 deletions lib/rdb_parser/redis_hash.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
defmodule RdbParser.RedisHash do
@moduledoc false

alias RdbParser.RedisList
alias RdbParser.RedisString

@spec parse_ziplist(binary) :: :incomplete | {map(), binary}
def parse_ziplist(binary) do
with {data_structure_length, rest} <- RdbParser.parse_length(binary),
<<ziplist::binary-size(data_structure_length), rest::binary>> <- rest,
list when is_list(list) <- RdbParser.RedisList.parse_ziplist(ziplist) do
list when is_list(list) <- RedisList.parse_ziplist(ziplist) do
map =
list
|> Enum.chunk_every(2)
Expand Down Expand Up @@ -36,8 +39,8 @@ defmodule RdbParser.RedisHash do
end

def parse_hash_entries(binary, parsed_entries, entries_left) do
with {key, rest} <- RdbParser.RedisString.parse(binary),
{value, rest} <- RdbParser.RedisString.parse(rest) do
with {key, rest} <- RedisString.parse(binary),
{value, rest} <- RedisString.parse(rest) do
parse_hash_entries(rest, [{key, value} | parsed_entries], entries_left - 1)
else
:incomplete -> :incomplete
Expand Down
5 changes: 3 additions & 2 deletions lib/rdb_parser/redis_set.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
defmodule RdbParser.RedisSet do
@moduledoc false
# Parses redis sets from the rdb format
# RedisSet.parse looks at the first byte to determine how the length is encoded, then takes the next
# RedisSet.parse looks at the first byte to determine how the length is encoded, then takes the
# length bytes and extracts the set values.

alias RdbParser.RedisString

@spec parse(binary) :: MapSet.t()
@spec parse(binary) :: :incomplete | MapSet.t()
def parse(binary) do
{num_entries, rest} = RdbParser.parse_length(binary)

Expand Down
6 changes: 3 additions & 3 deletions lib/rdb_parser/redis_string.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule RdbParser.RedisString do
@moduledoc false
# Parses redis strings from the rdb format
# RedisString.parse looks at the first byte to determine how the length is encoded, then takes the next
# length bytes as the string value and returns {string, rest}.
# RedisString.parse looks at the first byte to determine how the length is encoded, then takes the
# next length bytes as the string value and returns {string, rest}.

@enc_len_32 128
@enc_len_64 129
Expand All @@ -11,7 +11,7 @@ defmodule RdbParser.RedisString do
@enc_signed_32 194
@enc_lzf 195
# short string (6-bit length)
@spec parse(binary) :: binary | integer
@spec parse(binary) :: :incomplete | {integer, binary}
def parse(<<0::size(2), len::size(6), str::binary-size(len), rest::binary>>) do
{str, rest}
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule RdbParser.MixProject do
{:lzf, "~> 0.1"},
{:redix, ">= 0.0.0", only: [:test, :dev]},
{:dialyxir, "~> 0.5", only: [:dev]},
{:credo, "~> 0.8", only: [:dev]},
{:credo, "~> 0.9", only: [:dev]},
{:ex_doc, ">= 0.0.0", only: [:dev]}
]
end
Expand Down
3 changes: 2 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
"credo": {:hex, :credo, "0.8.10", "261862bb7363247762e1063713bb85df2bbd84af8d8610d1272cd9c1943bba63", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}], "hexpm"},
"credo": {:hex, :credo, "0.9.2", "841d316612f568beb22ba310d816353dddf31c2d94aa488ae5a27bb53760d0bf", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:poison, ">= 0.0.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.2.4", "99b637c62a4d65a20a9fb674b8cffb8baa771c04605a80c911c4418c69b75439", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.18.3", "f4b0e4a2ec6f333dccf761838a4b253d75e11f714b85ae271c9ae361367897b7", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"},
"lzf": {:hex, :lzf, "0.1.3", "fbc32bcfdf80ebe1e25419c483da479007eb657d7d2dc8712b7347c63c5681c6", [:mix], [], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"redix": {:hex, :redix, "0.7.0", "14a22cba1137600ccf8d64baf1b291689add779488f9a193d7d83dcc3e03a7a4", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
}
10 changes: 5 additions & 5 deletions test/rdb_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ defmodule RdbParserTest do
end

for integer <- [
-65537,
-65536,
-65535,
-65_537,
-65_536,
-65_535,
-257,
-256,
-255,
Expand All @@ -69,8 +69,8 @@ defmodule RdbParserTest do
13,
255,
256,
65535,
65536
65_535,
65_536
] do
test "parsing integer #{integer}", %{redis: redis} do
int = unquote(integer)
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule RdbParserTest.Support do
end)
end

def get_milliseconds() do
def get_milliseconds do
System.convert_time_unit(System.os_time(), :native, :milliseconds)
end

Expand Down

0 comments on commit 2d352cb

Please sign in to comment.