From 004847624bf953a8a5f480130fbd1ef39efc87cc Mon Sep 17 00:00:00 2001 From: Basile Burg Date: Mon, 8 May 2017 15:39:01 +0200 Subject: [PATCH 1/2] fix #352 - False positive: Parameter is never used, pointers --- src/analysis/unused.d | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/analysis/unused.d b/src/analysis/unused.d index 41ee57e..ad20543 100644 --- a/src/analysis/unused.d +++ b/src/analysis/unused.d @@ -300,16 +300,23 @@ class UnusedVariableCheck : BaseAnalyzer override void visit(const Parameter parameter) { import std.algorithm : canFind; + import std.algorithm.iteration : filter; + import std.range : empty; import std.array : array; if (parameter.name != tok!"") { - immutable bool isRef = canFind(parameter.parameterAttributes, cast(IdType) tok!"ref") - || canFind(parameter.parameterAttributes, - cast(IdType) tok!"in") || canFind(parameter.parameterAttributes, - cast(IdType) tok!"out"); + immutable bool isRef = + canFind(parameter.parameterAttributes, cast(IdType) tok!"ref") || + canFind(parameter.parameterAttributes, cast(IdType) tok!"in") || + canFind(parameter.parameterAttributes, cast(IdType) tok!"out"); + + immutable bool isPtr = parameter.type && !parameter.type + .typeSuffixes.filter!(a => a.star != tok!"").empty; + variableDeclared(parameter.name.text, parameter.name.line, - parameter.name.column, true, isRef); + parameter.name.column, true, isRef | isPtr); + if (parameter.default_ !is null) { interestDepth++; @@ -432,7 +439,7 @@ private: Regex!char re; } -unittest +@system unittest { import std.stdio : stderr; import analysis.config : StaticAnalysisConfig, Check; @@ -489,6 +496,18 @@ unittest a = 1; } + // Issue 352 + void test352_1() + { + void f(int *x) {*x = 1;} + } + + void test352_2() + { + void f(Bat** bat) {*bat = bats.ptr + 8;} + } + }c, sac); stderr.writeln("Unittest for UnusedVariableCheck passed."); } + From cd5dbd84e0519526cdc60f37cffeeefe044b5afc Mon Sep 17 00:00:00 2001 From: Basile Burg Date: Mon, 8 May 2017 18:24:15 +0200 Subject: [PATCH 2/2] in storage class doesn't mean ref/assignable --- src/analysis/unused.d | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/analysis/unused.d b/src/analysis/unused.d index ad20543..033fa82 100644 --- a/src/analysis/unused.d +++ b/src/analysis/unused.d @@ -299,18 +299,15 @@ class UnusedVariableCheck : BaseAnalyzer override void visit(const Parameter parameter) { - import std.algorithm : canFind; + import std.algorithm : among; import std.algorithm.iteration : filter; import std.range : empty; import std.array : array; if (parameter.name != tok!"") { - immutable bool isRef = - canFind(parameter.parameterAttributes, cast(IdType) tok!"ref") || - canFind(parameter.parameterAttributes, cast(IdType) tok!"in") || - canFind(parameter.parameterAttributes, cast(IdType) tok!"out"); - + immutable bool isRef = !parameter.parameterAttributes + .filter!(a => a.among(tok!"ref", tok!"out")).empty; immutable bool isPtr = parameter.type && !parameter.type .typeSuffixes.filter!(a => a.star != tok!"").empty; @@ -461,6 +458,8 @@ private: int a; // [warn]: Variable a is never used. } + void inPSC(in int a){} // [warn]: Parameter a is never used. + // Issue 380 int templatedEnum() {