Overview

edit

The 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, Directory or Environment method that has a FullPath equivalent, such as Path.Combine(fullPath, "file.txt") instead of fullPath / "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 string that 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

IdDescriptionSeverityCode fix
MFFP0001Use FullPath directly instead of Path.GetFullPathInfoYes
MFFP0002Use the right FullPath operand directlyInfoYes
MFFP0003Use '/' with a FullPath base instead of Path.GetFullPathInfoYes
MFFP0004Use '/' operator instead of Path.CombineInfoYes
MFFP0005Use FullPath.Name instead of Path.GetFileNameInfoYes
MFFP0006Use FullPath.NameWithoutExtension instead of Path.GetFileNameWithoutExtensionInfoYes
MFFP0007Use FullPath.Extension instead of Path.GetExtensionInfoYes
MFFP0008Use FullPath.Parent instead of Path.GetDirectoryNameInfoYes
MFFP0009Use FullPath.WithExtension instead of Path.ChangeExtensionInfoYes
MFFP0010Use FullPath.MakePathRelativeTo instead of Path.GetRelativePathInfoYes
MFFP0011Return FullPath instead of stringInfoNo
MFFP0012Declare the property as FullPath instead of stringInfoNo
MFFP0013Declare the variable as FullPath instead of stringInfoNo
MFFP0014Declare the parameter as FullPath instead of stringInfoNo
MFFP0015Compare FullPath values instead of their string representationWarningNo
MFFP0016FullPath.Equals with a string argument is always falseWarningNo
MFFP0018Path.IsPathRooted and Path.IsPathFullyQualified are redundant on a FullPathInfoYes
MFFP0019Simplify the FullPath.FromPath callInfoYes
MFFP0020Use FullPath.FromFileSystemInfo instead of FullPath.FromPathInfoYes
MFFP0021Use FullPath.IsEmptyInfoYes
MFFP0022Use FullPath.CreateParentDirectory instead of Directory.CreateDirectoryInfoYes
MFFP0023Use FullPath.Parent instead of Directory.GetParentInfoNo
MFFP0024Use the FullPath equivalent of Path.GetTempPath or Environment.GetFolderPathInfoYes

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.