Utilizing data flow analysis in an ElementProblemAnalyzer

In an ElementProblemAnalyzer, I want to detect if the return value of the base call is returned unchanged as method result.

For example:

protected override string M()
{
    return base.M();
}


or

protected override string M()
{
    var result = base.M();
    Console.WriteLine(result);
    return result;
}

But NOT in this case:

protected override string M()
{
    return base.M() ?? "x";
}


or

protected override string M()
{
    var result = base.M();
    if (Cond()) result = "x";
    return result;
}


Offers ReSharper's data flow analysis an API to do such a detection?

3 comments
Comment actions Permalink

Not directly, no. ReSharper has analyses that use the control flow graph to track null values and unused variables, etc. But nothing that would give you back this information. I think you could also use the control flow graph to figure out this information, but you'd have to do all the work yourself.

0
Comment actions Permalink

1. Theoretically, you can do your own dataflow inspector be inheriting T:JetBrains.ReSharper.Psi.ControlFlow.Impl.ControlFlowGraphInspector`1, defining what is the "context" of your inspection, how comtext merges and splits, manually collecting all the local variables and tracking values from base. invocations through context. That is really hard, but possible to do.

2. What you really need to do is "aliasing analysis" - tracking particular value through the sequence of assignments/initializations. ReSharper's C# dataflow inspector do not supports this kind of analysis (because we don't need it much) and it's really hard to make current C# dataflow inspector extensible without performance loss or compilcating it even more (it's already complex as hell).

3. Personally, I don't think you need dataflow analysis at all. It's MUCH easier to support only `return base.M();` case, as well as `var e = base.M(); ...; return e;` case simply by invoking Finder.FindAllReferences for `e` variable and checking if `e` has no write usages (smth like 'LanguageService.GetReferenceAccessType()').

0
Comment actions Permalink

Hi Matt & controlflow.

Many thanks for your answers! I just wanted to use the data flow analysis, if it would have been the easier option. As it's not, I'll go for the "pattern based" option 3. Thanks!

Could you give me one more hint? What's the best way to find all base calls in a method declaration's body? Do I have to manually search for IBaseExpression + the super member signature, or is there already something I can reuse?

0

Please sign in to leave a comment.