Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private CimMethodParametersCollection CreateParametersCollection(
{
string parameterName = enumerator.Key.ToString();

CimFlags parameterFlags = CimFlags.In;
const CimFlags parameterFlags = CimFlags.In;
object parameterValue = GetBaseObject(enumerator.Value);

DebugHelper.WriteLog(@"Create parameter name= {0}, value= {1}, flags= {2}.", 4,
Expand Down
16 changes: 8 additions & 8 deletions src/Microsoft.PowerShell.Commands.Diagnostics/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public static uint FormatMessageFromModule(uint lastError, string moduleName, ou

try
{
uint dwFormatFlags = FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE;
uint LANGID = (uint)GetUserDefaultLangID();
uint langError = (uint)Marshal.GetLastWin32Error();
if (langError != 0)
Expand All @@ -64,13 +63,14 @@ public static uint FormatMessageFromModule(uint lastError, string moduleName, ou
}

StringBuilder outStringBuilder = new(1024);
uint nChars = FormatMessage(dwFormatFlags,
moduleHandle,
lastError,
LANGID,
outStringBuilder,
(uint)outStringBuilder.Capacity,
IntPtr.Zero);
uint nChars = FormatMessage(
dwFlags: FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE,
lpSource: moduleHandle,
dwMessageId: lastError,
dwLanguageId: LANGID,
lpBuffer: outStringBuilder,
nSize: (uint)outStringBuilder.Capacity,
Arguments: IntPtr.Zero);

if (nChars == 0)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Microsoft.PowerShell.Commands.Diagnostics/PdhHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,8 +1057,6 @@ public uint GetValidPaths(string machineName,
ref StringCollection instances,
ref StringCollection validPaths)
{
uint res = PdhResults.PDH_CSTATUS_VALID_DATA;

PDH_COUNTER_PATH_ELEMENTS pathElts = new();
pathElts.MachineName = machineName;
pathElts.ObjectName = objectName;
Expand Down Expand Up @@ -1091,7 +1089,7 @@ public uint GetValidPaths(string machineName,
}
}

return res;
return PdhResults.PDH_CSTATUS_VALID_DATA;
}

public uint AddCounters(ref StringCollection validPaths, bool bFlushOldCounters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal static string WqlQueryAll(string from)
try
{
var type = typeof(T);
var binding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
const BindingFlags binding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
T rv = new T();

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

foreach (var instance in instances)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,12 @@ protected override void StopProcessing()
/// Run a command.
/// </summary>
protected void RunCommand(String command, String args) {
String cmd = string.Empty;

_process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/sbin/shutdown",
Arguments = cmd,
Arguments = string.Empty,
RedirectStandardOutput = false,
UseShellExecute = false,
CreateNoWindow = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,12 +914,11 @@ private CommandLineArguments ParseCompilerOption(IEnumerable<string> args)
{
string sdkDirectory = s_defaultSdkDirectory;
string baseDirectory = this.SessionState.Path.CurrentLocation.Path;
string additionalReferenceDirectories = null;

switch (Language)
{
case Language.CSharp:
return CSharpCommandLineParser.Default.Parse(args, baseDirectory, sdkDirectory, additionalReferenceDirectories);
return CSharpCommandLineParser.Default.Parse(args, baseDirectory, sdkDirectory);

default:
throw PSTraceSource.NewNotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private static void FilterActiveAssociationList(List<MshResolvedExpressionParame
// make sure we do not have too many properties

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

if (activeAssociationList.Count > nMax)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ private byte[] ConvertToBytes(object inputObject)
/// <param name="offset">Offset in the file.</param>
private void WriteHexadecimal(Span<byte> inputBytes, string path, long offset)
{
var bytesPerObject = 16;
const int bytesPerObject = 16;
for (int index = 0; index < inputBytes.Length; index += bytesPerObject)
{
var count = inputBytes.Length - index < bytesPerObject
Expand All @@ -508,7 +508,7 @@ private void WriteHexadecimal(Span<byte> inputBytes, string path, long offset)
/// </param>
private void WriteHexadecimal(Span<byte> inputBytes, long offset, string label)
{
var bytesPerObject = 16;
const int bytesPerObject = 16;
for (int index = 0; index < inputBytes.Length; index += bytesPerObject)
{
var count = inputBytes.Length - index < bytesPerObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ private void VerifyFont(Graphics g)
// we compute the length of two strings, one made of "large" characters
// one made of "narrow" ones. If they are the same length, we assume that
// the font is fixed pitch.
string large = "ABCDEF";
const string large = "ABCDEF";
float wLarge = g.MeasureString(large, _printFont).Width / large.Length;
string narrow = ".;'}l|";
const string narrow = ".;'}l|";
float wNarrow = g.MeasureString(narrow, _printFont).Width / narrow.Length;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ internal ErrorDetails GetErrorDetails(string errorId, params object[] args)

private ErrorRecord GetErrorNoCommandsImportedBecauseOfSkipping()
{
string errorId = "ErrorNoCommandsImportedBecauseOfSkipping";
const string errorId = "ErrorNoCommandsImportedBecauseOfSkipping";

ErrorDetails details = this.GetErrorDetails(errorId);

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

string errorId = "ErrorMalformedDataFromRemoteCommand";
const string errorId = "ErrorMalformedDataFromRemoteCommand";

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

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

string errorId = "ErrorCommandSkippedBecauseOfShadowing";
const string errorId = "ErrorCommandSkippedBecauseOfShadowing";

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

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

string errorId = "ErrorSkippedNonRequestedCommand";
const string errorId = "ErrorSkippedNonRequestedCommand";

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

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

string errorId = "ErrorSkippedNonRequestedTypeDefinition";
const string errorId = "ErrorSkippedNonRequestedTypeDefinition";

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

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

string errorId = "ErrorSkippedUnsafeCommandName";
const string errorId = "ErrorSkippedUnsafeCommandName";

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

Expand Down Expand Up @@ -771,7 +771,7 @@ private ErrorRecord GetErrorCouldntResolvedAlias(string aliasName)
throw PSTraceSource.NewArgumentNullException(nameof(aliasName));
}

string errorId = "ErrorCouldntResolveAlias";
const string errorId = "ErrorCouldntResolveAlias";

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

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

string errorId = "ErrorNoResultsFromRemoteEnd";
const string errorId = "ErrorNoResultsFromRemoteEnd";

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ protected override void ProcessRecord()

private void WritePathNotFoundError(string path)
{
var errorId = "PathNotFound";
var errorCategory = ErrorCategory.InvalidArgument;
const string errorId = "PathNotFound";
const ErrorCategory errorCategory = ErrorCategory.InvalidArgument;
var errorMessage = string.Format(UtilityCommonStrings.PathDoesNotExist, path);
var exception = new ArgumentException(errorMessage);
var errorRecord = new ErrorRecord(exception, errorId, errorCategory, path);
Expand All @@ -88,7 +88,7 @@ private void WritePathNotFoundError(string path)

private void WriteInvalidDataFileError(string resolvedPath, string errorId)
{
var errorCategory = ErrorCategory.InvalidData;
const ErrorCategory errorCategory = ErrorCategory.InvalidData;
var errorMessage = string.Format(UtilityCommonStrings.CouldNotParseAsPowerShellDataFile, resolvedPath);
var exception = new InvalidOperationException(errorMessage);
var errorRecord = new ErrorRecord(exception, errorId, errorCategory, resolvedPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private bool IsValidFileForUnblocking(string resolvedpath)
#if UNIX
private static bool IsBlocked(string path)
{
uint valueSize = 1024;
const uint valueSize = 1024;
IntPtr value = Marshal.AllocHGlobal((int)valueSize);
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,7 @@ internal void ParseLinkHeader(HttpResponseMessage response, System.Uri requestUr

// we only support the URL in angle brackets and `rel`, other attributes are ignored
// user can still parse it themselves via the Headers property
string pattern = "<(?<url>.*?)>;\\s*rel=(\"?)(?<rel>.*?)\\1[^\\w -.]?";
const string pattern = "<(?<url>.*?)>;\\s*rel=(\"?)(?<rel>.*?)\\1[^\\w -.]?";
IEnumerable<string> links;
if (response.Headers.TryGetValues("Link", out links))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ private static void CreateElevatedEntry(string title)
// Check startupInfo first to know if the current shell is interactive and owns a window before proceeding
// This check is fast (less than 1ms) and allows for quick-exit
GetStartupInfo(out StartUpInfo startupInfo);
var STARTF_USESHOWWINDOW = 0x00000001;
var SW_HIDE = 0;
const uint STARTF_USESHOWWINDOW = 0x00000001;
const ushort SW_HIDE = 0;
if (((startupInfo.dwFlags & STARTF_USESHOWWINDOW) == 1) && (startupInfo.wShowWindow != SW_HIDE))
{
string cmdPath = Assembly.GetEntryAssembly().Location.Replace(".dll", ".exe");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2477,7 +2477,7 @@ internal static Size GetLargestConsoleWindowSize(ConsoleHandle consoleHandle)
internal static string GetConsoleWindowTitle()
{
const int MaxWindowTitleLength = 1024;
DWORD bufferSize = MaxWindowTitleLength;
const DWORD bufferSize = MaxWindowTitleLength;
DWORD result;
StringBuilder consoleTitle = new StringBuilder((int)bufferSize);

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ private void DoRunspaceInitialization(bool skipProfiles, string initialCommand,
}
else
{
string shellId = "Microsoft.PowerShell";
const string shellId = "Microsoft.PowerShell";

// If the system lockdown policy says "Enforce", do so. Do this after types / formatting, default functions, etc
// are loaded so that they are trusted. (Validation of their signatures is done in F&O)
Expand Down Expand Up @@ -2029,7 +2029,7 @@ internal static string EscapeSingleQuotes(string str)

private void WriteErrorLine(string line)
{
ConsoleColor fg = ConsoleColor.Red;
const ConsoleColor fg = ConsoleColor.Red;
ConsoleColor bg = UI.RawUI.BackgroundColor;

UI.WriteLine(fg, bg, line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ public override
string fieldPrompt = null;
fieldPrompt = desc.Name;

bool fieldEchoOnPrompt = true;

// FieldDescription.ParameterAssemblyFullName never returns null. But this is
// defense in depth.
if (string.IsNullOrEmpty(desc.ParameterAssemblyFullName))
Expand Down Expand Up @@ -207,12 +205,22 @@ public override
{
fieldPromptList.Append(
string.Format(CultureInfo.InvariantCulture, "{0}]: ", inputList.Count));
bool inputListEnd = false;
bool endListInput = false;
object convertedObj = null;
string inputString = PromptForSingleItem(elementType, fieldPromptList.ToString(), fieldPrompt, caption, message,
desc, fieldEchoOnPrompt, true, out inputListEnd, out cancelInput, out convertedObj);

if (cancelInput || inputListEnd)
_ = PromptForSingleItem(
elementType,
fieldPromptList.ToString(),
fieldPrompt,
caption,
message,
desc,
fieldEchoOnPrompt: true,
listInput: true,
out endListInput,
out cancelInput,
out convertedObj);

if (cancelInput || endListInput)
{
break;
}
Expand Down Expand Up @@ -243,10 +251,20 @@ public override
fieldPrompt);
// field is not a list
object convertedObj = null;
bool dummy = false;

PromptForSingleItem(fieldType, printFieldPrompt, fieldPrompt, caption, message, desc,
fieldEchoOnPrompt, false, out dummy, out cancelInput, out convertedObj);
_ = PromptForSingleItem(
fieldType,
printFieldPrompt,
fieldPrompt,
caption,
message,
desc,
fieldEchoOnPrompt: true,
listInput: false,
endListInput: out _,
out cancelInput,
out convertedObj);

if (!cancelInput)
{
inputPSObject = PSObject.AsPSObject(convertedObj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ private void WriteChoicePrompt(string[,] hotkeysAndPlainLabels,
int lineLenMax = RawUI.WindowSize.Width - 1;
int lineLen = 0;

string choiceTemplate = "[{0}] {1} ";
const string choiceTemplate = "[{0}] {1} ";

for (int i = 0; i < hotkeysAndPlainLabels.GetLength(1); ++i)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,9 @@ internal override
{
nodesCompressed = 0;

int age = 0;

while (true)
{
ProgressNode node = FindOldestNodeOfGivenStyle(_topLevelNodes, age, priorStyle);
ProgressNode node = FindOldestNodeOfGivenStyle(_topLevelNodes, oldestSoFar: 0, priorStyle);
if (node == null)
{
// We've compressed every node of the prior style already.
Expand Down
Loading