Overview
editThe Meziantou.Framework.FullPath package includes Roslyn analyzers and code fixes for C#. They find the code that handles a FullPath as a string, and replace it with the FullPath members, operators and types. Nothing needs to be installed or configured: the rules are active in every C# project that references the package.
The rules fall into three groups:
- Using FullPath members: a
Path,DirectoryorEnvironmentmethod that has a FullPath equivalent, such asPath.Combine(fullPath, "file.txt")instead offullPath / "file.txt". Most of these rules have a code fix. - Declaring FullPath instead of string: a method, a property, a variable or a parameter declared as
stringthat only ever holds a FullPath. - Comparisons and checks: comparisons that compare the strings of the paths instead of the paths, and checks for the empty path. Two of these rules are warnings, because the code is likely wrong.
Rules
All the rules are enabled by default. There is no MFFP0017.
How a FullPath is recognized
The rules recognize a FullPath in all the forms that turn it into a string: the implicit conversion, a cast to string, Value, RawValue and ToString(). Path.GetFileName(fullPath), Path.GetFileName(fullPath.Value) and Path.GetFileName(fullPath.ToString()) are all reported, and all fixed to fullPath.Name.
The rules do nothing in a project that does not reference Meziantou.Framework.FullPath, and they skip generated code.
Apply the code fixes
The code fixes are offered by the light bulb of the IDE, like the fixes of the .NET analyzers. Every code fix supports Fix all, in a document, a project or a solution. Some fixes are not offered when they could change the behavior of the code; each rule lists these cases.
A fix often changes the type of an expression from string to FullPath. As FullPath converts implicitly to string, the code still compiles in most cases; once it does, the rules of Declaring FullPath instead of string find the declarations to change next.
Change the severity of a rule
Info rules appear as suggestions in the IDE and do not fail the build. To enforce a rule, raise its severity in the .editorconfig file of the solution, for instance dotnet_diagnostic.MFFP0004.severity = warning. Set it to none to disable a rule.