Skip to content
This repository was archived by the owner on Dec 17, 2018. It is now read-only.

Commit 0c2af3d

Browse files
connor rigbyConnorRigby
connor rigby
authored andcommittedJul 31, 2018
Add more checks to credo
1 parent 5fed361 commit 0c2af3d

File tree

6 files changed

+187
-48
lines changed

6 files changed

+187
-48
lines changed
 

‎.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- v3-plt-cache
2525
- run: mix dialyzer --plt
2626
- run: mix dialyzer --halt-exit-status
27-
- run: mix credo -a
27+
- run: mix credo --strict --ignore linelength -a
2828
- run:
2929
command: mix coveralls.circle
3030
environment:

‎.credo.exs

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# This file contains the configuration for Credo and you are probably reading
2+
# this after creating it with `mix credo.gen.config`.
3+
#
4+
# If you find anything wrong or unclear in this file, please report an
5+
# issue on GitHub: https://github.com/rrrene/credo/issues
6+
#
7+
%{
8+
#
9+
# You can have as many configs as you like in the `configs:` field.
10+
configs: [
11+
%{
12+
#
13+
# Run any exec using `mix credo -C <name>`. If no exec name is given
14+
# "default" is used.
15+
#
16+
name: "default",
17+
#
18+
# These are the files included in the analysis:
19+
files: %{
20+
#
21+
# You can give explicit globs or simply directories.
22+
# In the latter case `**/*.{ex,exs}` will be used.
23+
#
24+
included: ["lib/", "src/", "test/", "web/", "apps/"],
25+
excluded: [~r"/_build/", ~r"/deps/"]
26+
},
27+
#
28+
# If you create your own checks, you must specify the source files for
29+
# them here, so they can be loaded by Credo before running the analysis.
30+
#
31+
requires: [],
32+
#
33+
# If you want to enforce a style guide and need a more traditional linting
34+
# experience, you can change `strict` to `true` below:
35+
#
36+
strict: false,
37+
#
38+
# If you want to use uncolored output by default, you can change `color`
39+
# to `false` below:
40+
#
41+
color: true,
42+
#
43+
# You can customize the parameters of any check by adding a second element
44+
# to the tuple.
45+
#
46+
# To disable a check put `false` as second element:
47+
#
48+
# {Credo.Check.Design.DuplicatedCode, false}
49+
#
50+
checks: [
51+
#
52+
## Consistency Checks
53+
#
54+
{Credo.Check.Consistency.ExceptionNames},
55+
{Credo.Check.Consistency.LineEndings},
56+
{Credo.Check.Consistency.ParameterPatternMatching},
57+
{Credo.Check.Consistency.SpaceAroundOperators},
58+
{Credo.Check.Consistency.SpaceInParentheses},
59+
{Credo.Check.Consistency.TabsOrSpaces},
60+
61+
#
62+
## Design Checks
63+
#
64+
# You can customize the priority of any check
65+
# Priority values are: `low, normal, high, higher`
66+
#
67+
{Credo.Check.Design.AliasUsage, priority: :low},
68+
# For some checks, you can also set other parameters
69+
#
70+
# If you don't want the `setup` and `test` macro calls in ExUnit tests
71+
# or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just
72+
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
73+
#
74+
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},
75+
# You can also customize the exit_status of each check.
76+
# If you don't want TODO comments to cause `mix credo` to fail, just
77+
# set this value to 0 (zero).
78+
#
79+
{Credo.Check.Design.TagTODO, exit_status: 2},
80+
{Credo.Check.Design.TagFIXME},
81+
82+
#
83+
## Readability Checks
84+
#
85+
{Credo.Check.Readability.FunctionNames},
86+
{Credo.Check.Readability.LargeNumbers},
87+
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 80},
88+
{Credo.Check.Readability.ModuleAttributeNames},
89+
{Credo.Check.Readability.ModuleDoc},
90+
{Credo.Check.Readability.ModuleNames},
91+
{Credo.Check.Readability.ParenthesesOnZeroArityDefs},
92+
{Credo.Check.Readability.ParenthesesInCondition},
93+
{Credo.Check.Readability.PredicateFunctionNames},
94+
{Credo.Check.Readability.PreferImplicitTry},
95+
{Credo.Check.Readability.RedundantBlankLines},
96+
{Credo.Check.Readability.StringSigils},
97+
{Credo.Check.Readability.TrailingBlankLine},
98+
{Credo.Check.Readability.TrailingWhiteSpace},
99+
{Credo.Check.Readability.VariableNames},
100+
{Credo.Check.Readability.Semicolons},
101+
{Credo.Check.Readability.SpaceAfterCommas},
102+
103+
#
104+
## Refactoring Opportunities
105+
#
106+
{Credo.Check.Refactor.DoubleBooleanNegation},
107+
{Credo.Check.Refactor.CondStatements},
108+
{Credo.Check.Refactor.CyclomaticComplexity},
109+
{Credo.Check.Refactor.FunctionArity},
110+
{Credo.Check.Refactor.LongQuoteBlocks},
111+
{Credo.Check.Refactor.MatchInCondition},
112+
{Credo.Check.Refactor.NegatedConditionsInUnless},
113+
{Credo.Check.Refactor.NegatedConditionsWithElse},
114+
{Credo.Check.Refactor.Nesting},
115+
{Credo.Check.Refactor.PipeChainStart,
116+
excluded_argument_types: [:atom, :binary, :fn, :keyword], excluded_functions: []},
117+
{Credo.Check.Refactor.UnlessWithElse},
118+
119+
#
120+
## Warnings
121+
#
122+
{Credo.Check.Warning.BoolOperationOnSameValues},
123+
{Credo.Check.Warning.ExpensiveEmptyEnumCheck},
124+
{Credo.Check.Warning.IExPry},
125+
{Credo.Check.Warning.IoInspect},
126+
{Credo.Check.Warning.LazyLogging},
127+
{Credo.Check.Warning.OperationOnSameValues},
128+
{Credo.Check.Warning.OperationWithConstantResult},
129+
{Credo.Check.Warning.UnusedEnumOperation},
130+
{Credo.Check.Warning.UnusedFileOperation},
131+
{Credo.Check.Warning.UnusedKeywordOperation},
132+
{Credo.Check.Warning.UnusedListOperation},
133+
{Credo.Check.Warning.UnusedPathOperation},
134+
{Credo.Check.Warning.UnusedRegexOperation},
135+
{Credo.Check.Warning.UnusedStringOperation},
136+
{Credo.Check.Warning.UnusedTupleOperation},
137+
{Credo.Check.Warning.RaiseInsideRescue},
138+
139+
#
140+
# Controversial and experimental checks (opt-in, just remove `, false`)
141+
#
142+
{Credo.Check.Refactor.ABCSize, false},
143+
{Credo.Check.Refactor.AppendSingleItem, false},
144+
{Credo.Check.Refactor.VariableRebinding, false},
145+
{Credo.Check.Warning.MapGetUnsafePass, false},
146+
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},
147+
148+
#
149+
# Deprecated checks (these will be deleted after a grace period)
150+
#
151+
{Credo.Check.Readability.Specs, false}
152+
#
153+
# Custom checks can be created using `mix credo.gen.check`.
154+
#
155+
]
156+
}
157+
]
158+
}

‎lib/esqlite3/esqlite3_nif.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ defmodule Esqlite3Nif do
2323
@type bind_args :: [bind_arg]
2424

2525
@doc "Start a low level thread which will can handle sqlite3 calls."
26-
@spec start() :: {:ok, connection} | error_tup2
27-
def start(), do: :erlang.nif_error(:nif_library_not_loaded)
26+
@spec start :: {:ok, connection} | error_tup2
27+
def start, do: :erlang.nif_error(:nif_library_not_loaded)
2828

2929
@doc "Open the specified sqlite3 database."
3030
@spec open(connection, reference, pid, Path.t()) :: :ok | error_tup2

‎lib/sqlite.ex

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule Sqlite do
4141
"""
4242
@spec open(Keyword.t(), GenServer.options()) :: {:ok, conn} | {:error, term}
4343
def open(opts, gen_server_opts \\ []) when is_list(opts) do
44-
opts = Sqlite.Utils.default_opts(opts)
44+
opts = default_opts(opts)
4545

4646
case GenServer.start_link(Sqlite.Server, opts, gen_server_opts) do
4747
{:ok, pid} ->
@@ -238,4 +238,9 @@ defmodule Sqlite do
238238

239239
Keyword.merge(defaults, opts)
240240
end
241+
242+
@spec default_opts(Keyword.t()) :: Keyword.t()
243+
def default_opts(opts) do
244+
Keyword.merge([timeout: Application.get_env(:esqlite, :default_timeout, 5000)], opts)
245+
end
241246
end

‎lib/sqlite/server.ex

+20-36
Original file line numberDiff line numberDiff line change
@@ -38,56 +38,40 @@ defmodule Sqlite.Server do
3838

3939
@impl GenServer
4040
def handle_call({:query, sql, params, opts}, _from, state) do
41-
try do
42-
with {:ok, %Query{} = q} <- build_query(sql, opts, state.database),
43-
:ok <- Esqlite3.bind(q.statement, params) do
44-
r = q.statement |> Esqlite3.fetchall() |> build_result(q, state)
45-
{:reply, r, state}
46-
else
47-
err -> {:reply, error(err, state), state}
48-
end
49-
catch
41+
with {:ok, %Query{} = q} <- build_query(sql, opts, state.database),
42+
:ok <- Esqlite3.bind(q.statement, params) do
43+
r = q.statement |> Esqlite3.fetchall() |> build_result(q, state)
44+
{:reply, r, state}
45+
else
5046
err -> {:reply, error(err, state), state}
5147
end
5248
end
5349

5450
def handle_call({:release_query, query, opts}, _from, state) do
55-
try do
56-
case Esqlite3.reset(query.statement, opts[:timeout]) do
57-
:ok -> {:reply, :ok, state}
58-
err -> {:reply, error(err, state), state}
59-
end
60-
catch
51+
case Esqlite3.reset(query.statement, opts[:timeout]) do
52+
:ok -> {:reply, :ok, state}
6153
err -> {:reply, error(err, state), state}
6254
end
6355
end
6456

6557
def handle_call({:prepare, sql, opts}, _from, state) do
66-
try do
67-
case build_query(sql, opts, state.database) do
68-
{:ok, %Query{} = q} ->
69-
{:reply, {:ok, q}, state}
70-
71-
err ->
72-
{:reply, error(err, state), state}
73-
end
74-
catch
75-
err -> {:reply, error(err, state), state}
58+
case build_query(sql, opts, state.database) do
59+
{:ok, %Query{} = q} ->
60+
{:reply, {:ok, q}, state}
61+
62+
err ->
63+
{:reply, error(err, state), state}
7664
end
7765
end
7866

7967
def handle_call({:execute, query, params, opts}, _from, state) do
80-
try do
81-
case Esqlite3.bind(query.statement, params, opts[:timeout]) do
82-
:ok ->
83-
r = query.statement |> Esqlite3.fetchall() |> build_result(query, state)
84-
{:reply, r, state}
85-
86-
err ->
87-
{:reply, error(err, state), state}
88-
end
89-
catch
90-
err -> {:reply, error(err, state), state}
68+
case Esqlite3.bind(query.statement, params, opts[:timeout]) do
69+
:ok ->
70+
r = query.statement |> Esqlite3.fetchall() |> build_result(query, state)
71+
{:reply, r, state}
72+
73+
err ->
74+
{:reply, error(err, state), state}
9175
end
9276
end
9377

‎lib/sqlite/utils.ex

-8
This file was deleted.

0 commit comments

Comments
 (0)
This repository has been archived.