Skip to content

Commit c7bdb24

Browse files
authored
Mark local variable as const (#13217)
RCS1118: Mark local variable as const https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1118.md
1 parent 0e0cd0b commit c7bdb24

File tree

68 files changed

+244
-247
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+244
-247
lines changed

src/Microsoft.Management.Infrastructure.CimCmdlets/CimInvokeCimMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ private CimMethodParametersCollection CreateParametersCollection(
357357
{
358358
string parameterName = enumerator.Key.ToString();
359359

360-
CimFlags parameterFlags = CimFlags.In;
360+
const CimFlags parameterFlags = CimFlags.In;
361361
object parameterValue = GetBaseObject(enumerator.Value);
362362

363363
DebugHelper.WriteLog(@"Create parameter name= {0}, value= {1}, flags= {2}.", 4,

src/Microsoft.PowerShell.Commands.Diagnostics/CommonUtils.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public static uint FormatMessageFromModule(uint lastError, string moduleName, ou
5555

5656
try
5757
{
58-
uint dwFormatFlags = FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE;
5958
uint LANGID = (uint)GetUserDefaultLangID();
6059
uint langError = (uint)Marshal.GetLastWin32Error();
6160
if (langError != 0)
@@ -64,13 +63,14 @@ public static uint FormatMessageFromModule(uint lastError, string moduleName, ou
6463
}
6564

6665
StringBuilder outStringBuilder = new(1024);
67-
uint nChars = FormatMessage(dwFormatFlags,
68-
moduleHandle,
69-
lastError,
70-
LANGID,
71-
outStringBuilder,
72-
(uint)outStringBuilder.Capacity,
73-
IntPtr.Zero);
66+
uint nChars = FormatMessage(
67+
dwFlags: FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE,
68+
lpSource: moduleHandle,
69+
dwMessageId: lastError,
70+
dwLanguageId: LANGID,
71+
lpBuffer: outStringBuilder,
72+
nSize: (uint)outStringBuilder.Capacity,
73+
Arguments: IntPtr.Zero);
7474

7575
if (nChars == 0)
7676
{

src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,8 +1057,6 @@ public uint GetValidPaths(string machineName,
10571057
ref StringCollection instances,
10581058
ref StringCollection validPaths)
10591059
{
1060-
uint res = PdhResults.PDH_CSTATUS_VALID_DATA;
1061-
10621060
PDH_COUNTER_PATH_ELEMENTS pathElts = new();
10631061
pathElts.MachineName = machineName;
10641062
pathElts.ObjectName = objectName;
@@ -1091,7 +1089,7 @@ public uint GetValidPaths(string machineName,
10911089
}
10921090
}
10931091

1094-
return res;
1092+
return PdhResults.PDH_CSTATUS_VALID_DATA;
10951093
}
10961094

10971095
public uint AddCounters(ref StringCollection validPaths, bool bFlushOldCounters)

src/Microsoft.PowerShell.Commands.Management/commands/management/CIMHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal static string WqlQueryAll(string from)
8585
try
8686
{
8787
var type = typeof(T);
88-
var binding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
88+
const BindingFlags binding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
8989
T rv = new T();
9090

9191
using (var instance = session.QueryFirstInstance(nameSpace, CIMHelper.WqlQueryAll(wmiClassName)))
@@ -144,7 +144,7 @@ internal static string WqlQueryAll(string from)
144144
if (instances != null)
145145
{
146146
var type = typeof(T);
147-
var binding = BindingFlags.Public | BindingFlags.Instance;
147+
const BindingFlags binding = BindingFlags.Public | BindingFlags.Instance;
148148

149149
foreach (var instance in instances)
150150
{

src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,12 @@ protected override void StopProcessing()
154154
/// Run a command.
155155
/// </summary>
156156
protected void RunCommand(String command, String args) {
157-
String cmd = string.Empty;
158-
159157
_process = new Process()
160158
{
161159
StartInfo = new ProcessStartInfo
162160
{
163161
FileName = "/sbin/shutdown",
164-
Arguments = cmd,
162+
Arguments = string.Empty,
165163
RedirectStandardOutput = false,
166164
UseShellExecute = false,
167165
CreateNoWindow = true,

src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,12 +914,11 @@ private CommandLineArguments ParseCompilerOption(IEnumerable<string> args)
914914
{
915915
string sdkDirectory = s_defaultSdkDirectory;
916916
string baseDirectory = this.SessionState.Path.CurrentLocation.Path;
917-
string additionalReferenceDirectories = null;
918917

919918
switch (Language)
920919
{
921920
case Language.CSharp:
922-
return CSharpCommandLineParser.Default.Parse(args, baseDirectory, sdkDirectory, additionalReferenceDirectories);
921+
return CSharpCommandLineParser.Default.Parse(args, baseDirectory, sdkDirectory);
923922

924923
default:
925924
throw PSTraceSource.NewNotSupportedException();

src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/OutGridView/TableView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private static void FilterActiveAssociationList(List<MshResolvedExpressionParame
197197
// make sure we do not have too many properties
198198

199199
// NOTE: this is an arbitrary number, chosen to be a sensitive default
200-
int nMax = 256;
200+
const int nMax = 256;
201201

202202
if (activeAssociationList.Count > nMax)
203203
{

src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ private byte[] ConvertToBytes(object inputObject)
486486
/// <param name="offset">Offset in the file.</param>
487487
private void WriteHexadecimal(Span<byte> inputBytes, string path, long offset)
488488
{
489-
var bytesPerObject = 16;
489+
const int bytesPerObject = 16;
490490
for (int index = 0; index < inputBytes.Length; index += bytesPerObject)
491491
{
492492
var count = inputBytes.Length - index < bytesPerObject
@@ -508,7 +508,7 @@ private void WriteHexadecimal(Span<byte> inputBytes, string path, long offset)
508508
/// </param>
509509
private void WriteHexadecimal(Span<byte> inputBytes, long offset, string label)
510510
{
511-
var bytesPerObject = 16;
511+
const int bytesPerObject = 16;
512512
for (int index = 0; index < inputBytes.Length; index += bytesPerObject)
513513
{
514514
var count = inputBytes.Length - index < bytesPerObject

src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/out-printer/PrinterLineOutput.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ private void VerifyFont(Graphics g)
188188
// we compute the length of two strings, one made of "large" characters
189189
// one made of "narrow" ones. If they are the same length, we assume that
190190
// the font is fixed pitch.
191-
string large = "ABCDEF";
191+
const string large = "ABCDEF";
192192
float wLarge = g.MeasureString(large, _printFont).Width / large.Length;
193-
string narrow = ".;'}l|";
193+
const string narrow = ".;'}l|";
194194
float wNarrow = g.MeasureString(narrow, _printFont).Width / narrow.Length;
195195

196196
if (Math.Abs((float)(wLarge - wNarrow)) < 0.001F)
@@ -230,7 +230,7 @@ private void pd_PrintPage(object sender, PrintPageEventArgs ev)
230230
// on the first page we have to initialize the metrics for LineOutput
231231

232232
// work out the number of columns per page assuming fixed pitch font
233-
string s = "ABCDEF";
233+
const string s = "ABCDEF";
234234
float w = ev.Graphics.MeasureString(s, _printFont).Width / s.Length;
235235
float columnsPerPage = ev.MarginBounds.Width / w;
236236

src/Microsoft.PowerShell.Commands.Utility/commands/utility/ImplicitRemotingCommands.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ internal ErrorDetails GetErrorDetails(string errorId, params object[] args)
559559

560560
private ErrorRecord GetErrorNoCommandsImportedBecauseOfSkipping()
561561
{
562-
string errorId = "ErrorNoCommandsImportedBecauseOfSkipping";
562+
const string errorId = "ErrorNoCommandsImportedBecauseOfSkipping";
563563

564564
ErrorDetails details = this.GetErrorDetails(errorId);
565565

@@ -580,7 +580,7 @@ private ErrorRecord GetErrorMalformedDataFromRemoteCommand(string commandName)
580580
throw PSTraceSource.NewArgumentNullException(nameof(commandName));
581581
}
582582

583-
string errorId = "ErrorMalformedDataFromRemoteCommand";
583+
const string errorId = "ErrorMalformedDataFromRemoteCommand";
584584

585585
ErrorDetails details = this.GetErrorDetails(errorId, commandName);
586586

@@ -601,7 +601,7 @@ private ErrorRecord GetErrorCommandSkippedBecauseOfShadowing(string commandNames
601601
throw PSTraceSource.NewArgumentNullException(nameof(commandNames));
602602
}
603603

604-
string errorId = "ErrorCommandSkippedBecauseOfShadowing";
604+
const string errorId = "ErrorCommandSkippedBecauseOfShadowing";
605605

606606
ErrorDetails details = this.GetErrorDetails(errorId, commandNames);
607607

@@ -622,7 +622,7 @@ private ErrorRecord GetErrorSkippedNonRequestedCommand(string commandName)
622622
throw PSTraceSource.NewArgumentNullException(nameof(commandName));
623623
}
624624

625-
string errorId = "ErrorSkippedNonRequestedCommand";
625+
const string errorId = "ErrorSkippedNonRequestedCommand";
626626

627627
ErrorDetails details = this.GetErrorDetails(errorId, commandName);
628628

@@ -643,7 +643,7 @@ private ErrorRecord GetErrorSkippedNonRequestedTypeDefinition(string typeName)
643643
throw PSTraceSource.NewArgumentNullException(nameof(typeName));
644644
}
645645

646-
string errorId = "ErrorSkippedNonRequestedTypeDefinition";
646+
const string errorId = "ErrorSkippedNonRequestedTypeDefinition";
647647

648648
ErrorDetails details = this.GetErrorDetails(errorId, typeName);
649649

@@ -664,7 +664,7 @@ private ErrorRecord GetErrorSkippedUnsafeCommandName(string commandName)
664664
throw PSTraceSource.NewArgumentNullException(nameof(commandName));
665665
}
666666

667-
string errorId = "ErrorSkippedUnsafeCommandName";
667+
const string errorId = "ErrorSkippedUnsafeCommandName";
668668

669669
ErrorDetails details = this.GetErrorDetails(errorId, commandName);
670670

@@ -771,7 +771,7 @@ private ErrorRecord GetErrorCouldntResolvedAlias(string aliasName)
771771
throw PSTraceSource.NewArgumentNullException(nameof(aliasName));
772772
}
773773

774-
string errorId = "ErrorCouldntResolveAlias";
774+
const string errorId = "ErrorCouldntResolveAlias";
775775

776776
ErrorDetails details = this.GetErrorDetails(errorId, aliasName);
777777

@@ -792,7 +792,7 @@ private ErrorRecord GetErrorNoResultsFromRemoteEnd(string commandName)
792792
throw PSTraceSource.NewArgumentNullException(nameof(commandName));
793793
}
794794

795-
string errorId = "ErrorNoResultsFromRemoteEnd";
795+
const string errorId = "ErrorNoResultsFromRemoteEnd";
796796

797797
ErrorDetails details = this.GetErrorDetails(errorId, commandName);
798798

0 commit comments

Comments
 (0)