New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StringBuilder usage analyzer idea #1638
Comments
Started to give it a try yesterday, not yet finished. Hope to get something online this week. |
…-path tests Missing yet: - resources have no real values - RuleIds might have to be changed? - helpLinkUri s missing - VB variant not working - FixAll not working properly
Puhed my current state now - would be happy to get some more input, not expecting the PR to be merged in this state (see PR description) |
thanks @mavasani for giving the right hints on what to do.
thanks @mavasani for the hint.
This reverts commit 8dc25ab.
thanks @mavasani for the hint.
…uage agnostic a big big thanks to @CyrusNajmabadi for the great help. meanwhile solving some more review remarks: - using getInnerMostNodeForTie() on the context - return tasks directly instead of awaiting async methods
…cellationToken is ct, not ctx
- rename cancellationToken to ct (was ctx) - add explaining comments to code generation calls - improve reliability by save checking that the nodeToFix is an IInvocationExpression. This is checked on the Analyzer and a necessary condition to report the diagnostics, but it's not tightly coupled, so better be safe here.
covering named parameters in different order, method chainging of the stringBuilder.Append() and of
avoiding string checks
the one-parameter replacement sb.Append(xxxx.Substring(a)) to sb.Append(xxxx, a, xxxx.Length - a) is 1) dangerous when xxxx has side effects, as it's code is executed twice after the "fix", and 2) may have performance problems. Therefore this commit restricts to some Operation types that can be considered to be safe. provided substrings cover at least one case per operation kind that works. For the other OperationKinds, where no diagnostic must be reported I did not find ways yet how to create code that works reasonable. Happy to get hints here or advice what can safely be omitted as test cases. Precondition: The parameter must be of type string to be relevant for the code.
@mavasani does it also need to get through the |
Analyzer package
Could go into Microsoft.CodeQuality.Analyzers
Analyzer
Example: StringBuilder usage can be optimized in some API usage.
The analyzer would highlight 2 bad usages of substring as parameters to
StringBuilder.Append
, on thetext.Substring
calls.A code fix would be to transform these:
The end result of this analyzer would be removing intermediary string allocations.
The text was updated successfully, but these errors were encountered: