Skip to content

Commit 94a6ba4

Browse files
committed
Fix/suppress various FxCop errors
1 parent 56490af commit 94a6ba4

19 files changed

+255
-138
lines changed

PSReadLine/BasicEditing.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
--********************************************************************/
44

55
using System;
6+
using System.Diagnostics.CodeAnalysis;
7+
using System.Globalization;
68
using System.Linq;
79
using System.Management.Automation;
810
using System.Management.Automation.Language;
11+
using System.Management.Automation.Runspaces;
912

1013
namespace Microsoft.PowerShell
1114
{
@@ -14,6 +17,7 @@ public partial class PSConsoleReadLine
1417
/// <summary>
1518
/// Insert the key
1619
/// </summary>
20+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
1721
public static void SelfInsert(ConsoleKeyInfo? key = null, object arg = null)
1822
{
1923
if (!key.HasValue)
@@ -59,6 +63,7 @@ public static void SelfInsert(ConsoleKeyInfo? key = null, object arg = null)
5963
/// <summary>
6064
/// Reverts all of the input to the current input.
6165
/// </summary>
66+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
6267
public static void RevertLine(ConsoleKeyInfo? key = null, object arg = null)
6368
{
6469
if (_singleton._statusIsErrorMessage)
@@ -79,6 +84,7 @@ public static void RevertLine(ConsoleKeyInfo? key = null, object arg = null)
7984
/// Cancel the current input, leaving the input on the screen,
8085
/// but returns back to the host so the prompt is evaluated again.
8186
/// </summary>
87+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
8288
public static void CancelLine(ConsoleKeyInfo? key = null, object arg = null)
8389
{
8490
_singleton.ClearStatusMessage(false);
@@ -113,6 +119,7 @@ public static void CancelLine(ConsoleKeyInfo? key = null, object arg = null)
113119
/// Like ForwardKillLine - deletes text from the point to the end of the line,
114120
/// but does not put the deleted text in the kill ring.
115121
/// </summary>
122+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
116123
public static void ForwardDeleteLine(ConsoleKeyInfo? key = null, object arg = null)
117124
{
118125
var current = _singleton._current;
@@ -131,6 +138,7 @@ public static void ForwardDeleteLine(ConsoleKeyInfo? key = null, object arg = nu
131138
/// Like BackwardKillLine - deletes text from the point to the start of the line,
132139
/// but does not put the deleted text in the kill ring.
133140
/// </summary>
141+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
134142
public static void BackwardDeleteLine(ConsoleKeyInfo? key = null, object arg = null)
135143
{
136144
if (_singleton._current > 0)
@@ -146,6 +154,7 @@ public static void BackwardDeleteLine(ConsoleKeyInfo? key = null, object arg = n
146154
/// <summary>
147155
/// Delete the character before the cursor.
148156
/// </summary>
157+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
149158
public static void BackwardDeleteChar(ConsoleKeyInfo? key = null, object arg = null)
150159
{
151160
if (_singleton._visualSelectionCommandCount > 0)
@@ -195,6 +204,7 @@ private void DeleteCharImpl(bool orExit)
195204
/// <summary>
196205
/// Delete the character under the cursor.
197206
/// </summary>
207+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
198208
public static void DeleteChar(ConsoleKeyInfo? key = null, object arg = null)
199209
{
200210
_singleton.DeleteCharImpl(orExit: false);
@@ -203,6 +213,7 @@ public static void DeleteChar(ConsoleKeyInfo? key = null, object arg = null)
203213
/// <summary>
204214
/// Delete the character under the cursor, or if the line is empty, exit the process
205215
/// </summary>
216+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
206217
public static void DeleteCharOrExit(ConsoleKeyInfo? key = null, object arg = null)
207218
{
208219
_singleton.DeleteCharImpl(orExit: true);
@@ -284,7 +295,7 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
284295
if (commandInfo == null && !_singleton.UnresolvedCommandCouldSucceed(commandName, _rootAst))
285296
{
286297
_singleton._current = commandAst.CommandElements[0].Extent.EndOffset;
287-
detectedError = string.Format(PSReadLineResources.CommandNotFoundError, commandName);
298+
detectedError = string.Format(CultureInfo.CurrentCulture, PSReadLineResources.CommandNotFoundError, commandName);
288299
return AstVisitAction.StopVisit;
289300
}
290301
}
@@ -419,6 +430,7 @@ static bool StaticParameterBindingSupported(CommandInfo commandInfo)
419430
/// continuation prompt is displayed on the next line and PSReadline waits for
420431
/// keys to edit the current input.
421432
/// </summary>
433+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
422434
public static void AcceptLine(ConsoleKeyInfo? key = null, object arg = null)
423435
{
424436
_singleton.AcceptLineImpl(false);
@@ -430,6 +442,7 @@ public static void AcceptLine(ConsoleKeyInfo? key = null, object arg = null)
430442
/// continuation prompt is displayed on the next line and PSReadline waits for
431443
/// keys to edit the current input.
432444
/// </summary>
445+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
433446
public static void ValidateAndAcceptLine(ConsoleKeyInfo? key = null, object arg = null)
434447
{
435448
_singleton.AcceptLineImpl(true);
@@ -439,6 +452,7 @@ public static void ValidateAndAcceptLine(ConsoleKeyInfo? key = null, object arg
439452
/// Attempt to execute the current input. If it can be executed (like AcceptLine),
440453
/// then recall the next item from history the next time Readline is called.
441454
/// </summary>
455+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
442456
public static void AcceptAndGetNext(ConsoleKeyInfo? key = null, object arg = null)
443457
{
444458
if (_singleton.AcceptLineImpl(false))
@@ -459,6 +473,7 @@ public static void AcceptAndGetNext(ConsoleKeyInfo? key = null, object arg = nul
459473
/// keys to edit the current input. This is useful to enter multi-line input as
460474
/// a single command even when a single line is complete input by itself.
461475
/// </summary>
476+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
462477
public static void AddLine(ConsoleKeyInfo? key = null, object arg = null)
463478
{
464479
Insert('\n');

PSReadLine/Cmdlets.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
namespace Microsoft.PowerShell
1515
{
16+
17+
#pragma warning disable 1591
18+
1619
public enum TokenClassification
1720
{
1821
None,
@@ -193,6 +196,7 @@ public PSConsoleReadlineOptions(string hostName)
193196
/// odd things with script blocks, we create a white-list of commands
194197
/// that do invoke the script block - this covers the most useful cases.
195198
/// </summary>
199+
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
196200
public HashSet<string> CommandsToValidateScriptBlockArguments { get; set; }
197201

198202
/// <summary>
@@ -571,6 +575,7 @@ public class SetPSReadlineKeyHandlerCommand : PSCmdlet, IDynamicParameters
571575
[Parameter(Position = 0, Mandatory = true)]
572576
[Alias("Key")]
573577
[ValidateNotNullOrEmpty]
578+
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
574579
public string[] Chord { get; set; }
575580

576581
[Parameter(Position = 1, Mandatory = true, ParameterSetName = "ScriptBlock")]
@@ -692,6 +697,7 @@ public class RemoveKeyHandlerCommand : PSCmdlet
692697
[Parameter(Position = 0, Mandatory = true)]
693698
[Alias("Key")]
694699
[ValidateNotNullOrEmpty]
700+
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
695701
public string[] Chord { get; set; }
696702

697703
[ExcludeFromCodeCoverage]
@@ -700,4 +706,7 @@ protected override void EndProcessing()
700706
PSConsoleReadLine.RemoveKeyHandler(Chord);
701707
}
702708
}
709+
710+
#pragma warning restore 1591
711+
703712
}

PSReadLine/Completion.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Generic;
88
using System.Diagnostics;
99
using System.Diagnostics.CodeAnalysis;
10+
using System.Globalization;
1011
using System.Linq;
1112
using System.Management.Automation;
1213
using System.Management.Automation.Runspaces;
@@ -34,6 +35,7 @@ CommandCompletion IPSConsoleReadLineMockableMethods.CompleteInput(string input,
3435
/// Attempt to complete the text surrounding the cursor with the next
3536
/// available completion.
3637
/// </summary>
38+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
3739
public static void TabCompleteNext(ConsoleKeyInfo? key = null, object arg = null)
3840
{
3941
_singleton.Complete(forward: true);
@@ -43,6 +45,7 @@ public static void TabCompleteNext(ConsoleKeyInfo? key = null, object arg = null
4345
/// Attempt to complete the text surrounding the cursor with the previous
4446
/// available completion.
4547
/// </summary>
48+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
4649
public static void TabCompletePrevious(ConsoleKeyInfo? key = null, object arg = null)
4750
{
4851
_singleton.Complete(forward: false);
@@ -87,6 +90,7 @@ private static string GetUnquotedText(string s, bool consistentQuoting)
8790
/// prefix is used for completion. If trying to complete the longest
8891
/// unambiguous completion, a list of possible completions is displayed.
8992
/// </summary>
93+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
9094
public static void Complete(ConsoleKeyInfo? key = null, object arg = null)
9195
{
9296
_singleton.CompleteImpl(key, arg, false);
@@ -98,11 +102,13 @@ public static void Complete(ConsoleKeyInfo? key = null, object arg = null)
98102
/// prefix is used for completion. If trying to complete the longest
99103
/// unambiguous completion, a list of possible completions is displayed.
100104
/// </summary>
105+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
101106
public static void MenuComplete(ConsoleKeyInfo? key = null, object arg = null)
102107
{
103108
_singleton.CompleteImpl(key, arg, true);
104109
}
105110

111+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
106112
private void CompleteImpl(ConsoleKeyInfo? key, object arg, bool menuSelect)
107113
{
108114
var completions = GetCompletions();
@@ -267,13 +273,13 @@ private void DoReplacementForCompletion(CompletionResult completionResult, Comma
267273

268274
private static string GetReplacementTextForDirectory(string replacementText, ref int cursorAdjustment)
269275
{
270-
if (!replacementText.EndsWith("\\"))
276+
if (!replacementText.EndsWith("\\", StringComparison.Ordinal))
271277
{
272-
if (replacementText.EndsWith("\\'") || replacementText.EndsWith("\\\""))
278+
if (replacementText.EndsWith("\\'", StringComparison.Ordinal) || replacementText.EndsWith("\\\"", StringComparison.Ordinal))
273279
{
274280
cursorAdjustment = -1;
275281
}
276-
else if (replacementText.EndsWith("'") || replacementText.EndsWith("\""))
282+
else if (replacementText.EndsWith("'", StringComparison.Ordinal) || replacementText.EndsWith("\"", StringComparison.Ordinal))
277283
{
278284
var len = replacementText.Length;
279285
replacementText = replacementText.Substring(0, len - 1) + '\\' + replacementText[len - 1];
@@ -303,6 +309,7 @@ private static void InvertSelectedCompletion(CHAR_INFO[] buffer, int selectedIte
303309
/// <summary>
304310
/// Display the list of possible completions.
305311
/// </summary>
312+
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
306313
public static void PossibleCompletions(ConsoleKeyInfo? key = null, object arg = null)
307314
{
308315
var completions = _singleton.GetCompletions();
@@ -330,7 +337,7 @@ private void PossibleCompletionsImpl(CommandCompletion completions, bool menuSel
330337

331338
if (completions.CompletionMatches.Count >= _options.CompletionQueryItems)
332339
{
333-
if (!PromptYesOrNo(string.Format(PSReadLineResources.DisplayAllPossibilities, completions.CompletionMatches.Count)))
340+
if (!PromptYesOrNo(string.Format(CultureInfo.CurrentCulture, PSReadLineResources.DisplayAllPossibilities, completions.CompletionMatches.Count)))
334341
{
335342
return;
336343
}

PSReadLine/ConsoleKeyChordConverter.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.Globalization;
78
using System.Linq;
89
using System.Runtime.InteropServices;
910

1011
namespace Microsoft.PowerShell
1112
{
13+
/// <summary>
14+
/// A helper class for converting strings to ConsoleKey chords.
15+
/// </summary>
1216
public static class ConsoleKeyChordConverter
1317
{
1418
/// <summary>
@@ -31,7 +35,7 @@ public static ConsoleKeyInfo[] Convert(string chord)
3135

3236
if (tokens.Length > 2)
3337
{
34-
throw new ArgumentException("Chord can have at most two keys");
38+
throw new ArgumentException(PSReadLineResources.ChordWIthTooManyKeys);
3539
}
3640

3741
var result = new ConsoleKeyInfo[tokens.Length];
@@ -85,8 +89,7 @@ private static ConsoleKeyInfo ConvertOneSequence(string sequence)
8589
}
8690
else
8791
{
88-
throw new ArgumentException("Unrecognized key '" + token + "'. Please use a character literal or a " +
89-
"well-known key name from the System.ConsoleKey enumeration.");
92+
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, PSReadLineResources.UnrecognizedKey, token));
9093
}
9194
}
9295
// try simple parse for ConsoleKey enum name
@@ -100,15 +103,13 @@ private static ConsoleKeyInfo ConvertOneSequence(string sequence)
100103

101104
if (!valid)
102105
{
103-
throw new ArgumentException(String.Format("Unable to translate '{0}' to " +
104-
"virtual key code: {1}.", token[0], failReason));
106+
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, PSReadLineResources.CantTranslateKey, token[0], failReason));
105107
}
106108
}
107109

108110
if (!valid)
109111
{
110-
throw new ArgumentException("Unrecognized key '" + token + "'. Please use a character literal or a " +
111-
"well-known key name from the System.ConsoleKey enumeration.");
112+
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, PSReadLineResources.UnrecognizedKey, token));
112113
}
113114
}
114115
else
@@ -130,21 +131,20 @@ private static ConsoleKeyInfo ConvertOneSequence(string sequence)
130131
// either found duplicate modifier token or shift state
131132
// was already implied from char, e.g. char is "}", which is "shift+]"
132133
throw new ArgumentException(
133-
String.Format("Duplicate or invalid modifier token '{0}' for key '{1}'.", modifier, key));
134+
String.Format(CultureInfo.CurrentCulture, PSReadLineResources.InvalidModifier, modifier, key));
134135
}
135136
modifiers |= modifier;
136137
}
137138
else
138139
{
139-
throw new ArgumentException("Invalid modifier token '" + token + "'. The supported modifiers are " +
140-
"'alt', 'shift', 'control' or 'ctrl'.");
140+
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, PSReadLineResources.InvalidModifier, token));
141141
}
142142
}
143143
}
144144

145145
if (!valid)
146146
{
147-
throw new ArgumentException("Invalid sequence '" + sequence + "'.");
147+
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, PSReadLineResources.InvalidSequence, sequence));
148148
}
149149

150150
char keyChar = GetCharFromConsoleKey(key, modifiers);
@@ -161,6 +161,7 @@ private static bool TryParseCharLiteral(char literal, ref ConsoleModifiers modif
161161

162162
// shift state will be in MSB
163163
short virtualKey = NativeMethods.VkKeyScan(literal);
164+
int hresult = Marshal.GetLastWin32Error();
164165

165166
if (virtualKey != 0)
166167
{
@@ -192,13 +193,11 @@ private static bool TryParseCharLiteral(char literal, ref ConsoleModifiers modif
192193
else
193194
{
194195
// haven't seen this happen yet, but possible
195-
failReason = String.Format("The virtual key code {0} does not map " +
196-
"to a known System.ConsoleKey enumerated value.", virtualKey);
196+
failReason = String.Format(CultureInfo.CurrentCulture, PSReadLineResources.UnrecognizedKey, virtualKey);
197197
}
198198
}
199199
else
200200
{
201-
int hresult = Marshal.GetLastWin32Error();
202201
Exception e = Marshal.GetExceptionForHR(hresult);
203202
failReason = e.Message;
204203
}

0 commit comments

Comments
 (0)