Comments (9)
The diagnostic is reported on the operation. In this case this is an IInvocationOperation
, so the associated syntax is the InvocationExpressionSyntax
. This includes the target (everything before (
, not just the method name) and the arguments. Here's an example of what it means:
_ = "test".Substring(0, 1).Replace(" ", " ").Trim()
^--------------------------------------^
I don't know what the standard way is, but this is the easiest! If you want to change the reported location, there are multiple impacts:
- The analyzer must start dealing with the syntax API instead of using the Operation API (the generic way). This means your code is now specific for C# or VB.NET and is more subject to bugs and issues when a new version of the language is released.
- The fixer needs to find the full context again instead of having the right expression immediately
So, this increases the code complexity.
from meziantou.analyzer.
Thanks for your quick reply!
I really don't know enough about how analyzers work internally to comment. As a user, I found the demonstrated behavior confusing; I actually dug around for a bit before figuring out that in fact, there are no issues in the preceding lines. If it would be disproportionally hard to fix this behavior then please disregard it.
from meziantou.analyzer.
I don't think I'll change the reported location, unless the change is easier than I think. Note, that I can also improve the error message to include the replaced characters. This would reduce the ambiguity.
By curiosity, which location do you expect to be reported? The method name? The arguments? The method name and arguments? Something else?
from meziantou.analyzer.
The message including the characters would help a lot.
I'd expect the squiggly lines to appear only on the line of the violation, i.e. the 4th line in the example (and if the whole statement would be a single line then also only under the offending Replace()
, though I guess that's stretching what's feasible).
from meziantou.analyzer.
After looking at the code,
- I've updated the reported location of methods with a single argument (
StartsWith
,EndsWith
,IndexOf
,LastIndexOf
,Join
). Replace
will still report the full invocation. I think it would be possible to report both arguments using "AdditionalLocation", but I've never use them, so I'm not sure how it will behave in VS or Rider.- I've implemented a CodeFixer, so it's easy to apply the fix (and view the diff before applying the fix).
from meziantou.analyzer.
Thank you! Didn't you also want to update the error message?
from meziantou.analyzer.
Also, in the above example, the code fix will produce this code:
var replaced = "test"
.Replace("string 1", "string 2", StringComparison.Ordinal)
.Replace("string 3", "string 4", StringComparison.Ordinal)
.Replace('\n', 'n', StringComparison.Ordinal);
This is invalid, since string Replace(char oldChar, char newChar)
doesn't have an overload with a StringComparison
parameter.
from meziantou.analyzer.
Thanks for reporting the bug. I've improved the tests to detect wrong fixes. The new flow for all tests is:
- Compile the source code to validate it is valid
- Run analyzers and validate reported locations
- Apply code fixes if any
- (new) Compile the fixed code
- Validate the fixed code is the one expected
I've detected bugs in 3 code fixers!
Didn't you also want to update the error message
Nope, but the diagnostic is now reported at the method name location.
from meziantou.analyzer.
Thank you for the quick fix!
I see, I did look for the reporting location changing, but didn't notice anything, after multiple rebuild/clean cycles even, thinking that VS didn't notice the analyzer package being updated. Now after the v2.0.58 update it works,thank you!
from meziantou.analyzer.
Related Issues (20)
- MA0147 clarifications HOT 3
- MA0004 conflicts with xUnit1030 HOT 4
- `MA0051` Should not warn with a huge switch expression HOT 1
- Context detection issue in MA0004 HOT 1
- Analyze usage of CultureInfo ctor HOT 1
- MA0011: false positives reported
- Support member exclusion filters for rules
- [Question]: Is there any way to enable all rules in this package? HOT 2
- MA0143 - False Positive is Dispose Method HOT 3
- MA0142 & MA0141 and Linq
- MA0134 - false positive in Expression<Func<Task>>
- Blacklist for LoggerParameters (Placeholders) HOT 4
- Remove rules with a Roslyn Equivalent HOT 2
- Most of the Blazor rules do not work against App on .net 8.0 HOT 7
- MA0010 - Should be deprecated in favor of CA1018 HOT 1
- MA0047 - Should be deprecated in favor of CA1050 HOT 4
- Rule for consistent formatting of comments HOT 1
- `MA0138` does not account for `IAsyncEnumerable<T>` HOT 3
- Do not warn `MA0155` for methods which get subscribed to an event HOT 1
- [MA0151] False warning when use `typeof` keyword HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from meziantou.analyzer.