From b5bd76dfa8f4229aab30f9cf1ed57b40d035b3f9 Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Thu, 20 Aug 2020 21:15:58 +0300 Subject: [PATCH 1/2] Useless Assign: ignore variables with '_' prefix closes #164 --- spec/ameba/ast/variabling/variable_spec.cr | 17 +++++++++++++++++ spec/ameba/rule/lint/useless_assign_spec.cr | 8 ++++++++ src/ameba/ast/variabling/variable.cr | 5 +++++ src/ameba/rule/lint/useless_assign.cr | 2 +- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/spec/ameba/ast/variabling/variable_spec.cr b/spec/ameba/ast/variabling/variable_spec.cr index 6f7f1c7b..fc39ef0d 100644 --- a/spec/ameba/ast/variabling/variable_spec.cr +++ b/spec/ameba/ast/variabling/variable_spec.cr @@ -140,6 +140,23 @@ module Ameba::AST end end + describe "#ignored?" do + it "is true if variable is ignored" do + variable = Variable.new(Crystal::Var.new("_var"), scope) + variable.ignored?.should be_true + end + + it "is false if variable is not ignored" do + variable = Variable.new(Crystal::Var.new("v_ar"), scope) + variable.ignored?.should be_false + end + + it "is true if variable is a black hole" do + variable = Variable.new(Crystal::Var.new("_"), scope) + variable.ignored?.should be_true + end + end + describe "#eql?" do var = Crystal::Var.new("foo").at(Crystal::Location.new(nil, 1, 2)) variable = Variable.new var, scope diff --git a/spec/ameba/rule/lint/useless_assign_spec.cr b/spec/ameba/rule/lint/useless_assign_spec.cr index 34b50451..bd5fdb2d 100644 --- a/spec/ameba/rule/lint/useless_assign_spec.cr +++ b/spec/ameba/rule/lint/useless_assign_spec.cr @@ -54,6 +54,14 @@ module Ameba::Rule::Lint subject.catch(s).should_not be_valid end + it "does not report ignored assigments" do + s = Source.new %( + payload, _header = decode + puts payload + ) + subject.catch(s).should be_valid + end + it "reports a useless assignment in a proc inside a block" do s = Source.new %( def method diff --git a/src/ameba/ast/variabling/variable.cr b/src/ameba/ast/variabling/variable.cr index 7e8d9af2..1a7eab42 100644 --- a/src/ameba/ast/variabling/variable.cr +++ b/src/ameba/ast/variabling/variable.cr @@ -143,6 +143,11 @@ module Ameba::AST end end + # Returns true if the name starts with '_', false if not. + def ignored? + name.starts_with? '_' + end + # Returns true if the `node` represents exactly # the same Crystal node as `@node`. def eql?(node) diff --git a/src/ameba/rule/lint/useless_assign.cr b/src/ameba/rule/lint/useless_assign.cr index 7c6c4b79..bff18188 100644 --- a/src/ameba/rule/lint/useless_assign.cr +++ b/src/ameba/rule/lint/useless_assign.cr @@ -39,7 +39,7 @@ module Ameba::Rule::Lint def test(source, node, scope : AST::Scope) scope.variables.each do |var| - next if var.captured_by_block? || var.used_in_macro? + next if var.captured_by_block? || var.used_in_macro? || var.ignored? var.assignments.each do |assign| next if assign.referenced? || assign.transformed? From 5ee54293a8f08bf39f5deb6cbdbd7c919eb71da6 Mon Sep 17 00:00:00 2001 From: Vitalii Elenhaupt Date: Wed, 26 Aug 2020 22:42:29 +0300 Subject: [PATCH 2/2] Upgrade alpine img refs #166 --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index bb8a924e..cb448078 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,11 @@ -FROM alpine:3.8 as builder +FROM alpine:3.12 as builder RUN apk add --update crystal shards openssl-dev yaml-dev musl-dev make RUN mkdir /ameba WORKDIR /ameba COPY . /ameba/ RUN make clean && make -FROM alpine:3.8 +FROM alpine:3.12 RUN apk add --update openssl yaml pcre gc libevent libgcc RUN mkdir /src WORKDIR /src