Skip to content

Commit df2e4d5

Browse files
committed
Fix CA1854
1 parent 30463ab commit df2e4d5

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

Src/DLR

Src/IronPython.Modules/ResourceMetaPathImporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ fully qualified (dotted) module name. It returns the imported
8888
)]
8989
public object load_module(CodeContext /*!*/ context, string fullname) {
9090
var modules = context.LanguageContext.SystemStateModules;
91-
if (modules.ContainsKey(fullname))
92-
return modules[fullname];
91+
if (modules.TryGetValue(fullname, out var module))
92+
return module;
9393

9494
bool ispackage;
9595
string modpath;

Src/IronPython.Modules/_csv.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,9 @@ public static Dialect Create(CodeContext/*!*/ context,
285285
kwArgs.TryGetValue("strict", out strict);
286286

287287
if (dialect != null) {
288-
if (dialect is string) {
289-
string dialectName = (string)dialect;
290-
if (dialects.ContainsKey(dialectName))
291-
dialect = dialects[dialectName];
288+
if (dialect is string dialectName) {
289+
if (dialects.TryGetValue(dialectName, out var value))
290+
dialect = value;
292291
else
293292
throw MakeError("unknown dialect");
294293
}

Src/IronPython.Modules/re.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,8 @@ static string ApplyVerbose(string pattern) {
10621062
if (tmpIndex == -1) throw PythonExceptions.CreateThrowable(error(context), "unexpected end of regex");
10631063

10641064
var namedGroup = pattern.Substring(nameIndex + 2, tmpIndex - (nameIndex + 2));
1065-
if (namedGroups.ContainsKey(namedGroup)) {
1066-
throw PythonExceptions.CreateThrowable(error(context), $"redefinition of group name '{namedGroup}' as group {groupCount}; was group {namedGroups[namedGroup]}");
1065+
if (namedGroups.TryGetValue(namedGroup, out var value)) {
1066+
throw PythonExceptions.CreateThrowable(error(context), $"redefinition of group name '{namedGroup}' as group {groupCount}; was group {value}");
10671067
}
10681068

10691069
namedGroups[namedGroup] = groupCount;

Src/IronPython/Hosting/PythonOptionsParser.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,23 +205,24 @@ protected override void ParseArgument(string/*!*/ arg) {
205205
}
206206

207207
protected override void HandleImplementationSpecificOption(string arg, string val) {
208+
object frames;
208209
switch (arg) {
209210
case "NoFrames":
210-
if (LanguageSetup.Options.ContainsKey("Frames") && LanguageSetup.Options["Frames"] != ScriptingRuntimeHelpers.False) {
211+
if (LanguageSetup.Options.TryGetValue("Frames", out frames) && frames != ScriptingRuntimeHelpers.False) {
211212
throw new InvalidOptionException("Only one of -X [Full]Frames/NoFrames may be specified");
212213
}
213214
LanguageSetup.Options["Frames"] = ScriptingRuntimeHelpers.False;
214215
break;
215216

216217
case "Frames":
217-
if (LanguageSetup.Options.ContainsKey("Frames") && LanguageSetup.Options["Frames"] != ScriptingRuntimeHelpers.True) {
218+
if (LanguageSetup.Options.TryGetValue("Frames", out frames) && frames != ScriptingRuntimeHelpers.True) {
218219
throw new InvalidOptionException("Only one of -X [Full]Frames/NoFrames may be specified");
219220
}
220221
LanguageSetup.Options["Frames"] = ScriptingRuntimeHelpers.True;
221222
break;
222223

223224
case "FullFrames":
224-
if (LanguageSetup.Options.ContainsKey("Frames") && LanguageSetup.Options["Frames"] != ScriptingRuntimeHelpers.True) {
225+
if (LanguageSetup.Options.TryGetValue("Frames", out frames) && frames != ScriptingRuntimeHelpers.True) {
225226
throw new InvalidOptionException("Only one of -X [Full]Frames/NoFrames may be specified");
226227
}
227228
LanguageSetup.Options["Frames"] = LanguageSetup.Options["FullFrames"] = ScriptingRuntimeHelpers.True;

Src/IronPythonTest/Cases/CaseExecuter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ private static string GetIronPythonPath() {
148148
return string.Empty;
149149
}
150150

151-
private string ReplaceVariables(string input, IDictionary<string, string> replacements) {
151+
private string ReplaceVariables(string input, Dictionary<string, string> replacements) {
152152
Regex variableRegex = new Regex(@"\$\(([^}]+)\)", RegexOptions.Compiled);
153153

154154
var result = input;
155155
var match = variableRegex.Match(input);
156156
while (match.Success) {
157157
var variable = match.Groups[1].Value;
158-
if (replacements.ContainsKey(variable)) {
159-
result = result.Replace(match.Groups[0].Value, replacements[variable]);
158+
if (replacements.TryGetValue(variable, out var replacement)) {
159+
result = result.Replace(match.Groups[0].Value, replacement);
160160
}
161161
match = match.NextMatch();
162162
}

0 commit comments

Comments
 (0)