Skip to content

Commit f444e58

Browse files
ncaveKevinRansom
authored andcommitted
Uniform TryGetValue usage (#6598)
* Uniform TryGetValue usage * Removed extra parenthesis * Remove parenthesis
1 parent c4c1d9c commit f444e58

22 files changed

+103
-105
lines changed

fcs/docsrc/content/filesystem.fsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ let B = File1.A + File1.A"""
4141
interface IFileSystem with
4242
// Implement the service to open files for reading and writing
4343
member __.FileStreamReadShim(fileName) =
44-
match files.TryGetValue(fileName) with
44+
match files.TryGetValue fileName with
4545
| true, text -> new MemoryStream(Encoding.UTF8.GetBytes(text)) :> Stream
4646
| _ -> defaultFileSystem.FileStreamReadShim(fileName)
4747

@@ -52,7 +52,7 @@ let B = File1.A + File1.A"""
5252
defaultFileSystem.FileStreamWriteExistingShim(fileName)
5353

5454
member __.ReadAllBytesShim(fileName) =
55-
match files.TryGetValue(fileName) with
55+
match files.TryGetValue fileName with
5656
| true, text -> Encoding.UTF8.GetBytes(text)
5757
| _ -> defaultFileSystem.ReadAllBytesShim(fileName)
5858

fcs/docsrc/content/ja/filesystem.fsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ let B = File1.A + File1.A"""
4141
interface IFileSystem with
4242
// 読み取りおよび書き込み用にファイルをオープンする機能を実装
4343
member __.FileStreamReadShim(fileName) =
44-
match files.TryGetValue(fileName) with
44+
match files.TryGetValue fileName with
4545
| true, text -> new MemoryStream(Encoding.UTF8.GetBytes(text)) :> Stream
4646
| _ -> defaultFileSystem.FileStreamReadShim(fileName)
4747

@@ -55,7 +55,7 @@ let B = File1.A + File1.A"""
5555
defaultFileSystem.FileStreamWriteExistingShim(fileName)
5656

5757
member __.ReadAllBytesShim(fileName) =
58-
match files.TryGetValue(fileName) with
58+
match files.TryGetValue fileName with
5959
| true, text -> Encoding.UTF8.GetBytes(text)
6060
| _ -> defaultFileSystem.ReadAllBytesShim(fileName)
6161

src/absil/illib.fs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -985,15 +985,13 @@ type MemoizationTable<'T, 'U>(compute: 'T -> 'U, keyComparer: IEqualityComparer<
985985

986986
member t.Apply x =
987987
if (match canMemoize with None -> true | Some f -> f x) then
988-
let mutable res = Unchecked.defaultof<'U>
989-
let ok = table.TryGetValue(x, &res)
990-
if ok then res
991-
else
988+
match table.TryGetValue x with
989+
| true, res -> res
990+
| _ ->
992991
lock table (fun () ->
993-
let mutable res = Unchecked.defaultof<'U>
994-
let ok = table.TryGetValue(x, &res)
995-
if ok then res
996-
else
992+
match table.TryGetValue x with
993+
| true, res -> res
994+
| _ ->
997995
let res = compute x
998996
table.[x] <- res
999997
res)
@@ -1074,11 +1072,10 @@ module Tables =
10741072
let memoize f =
10751073
let t = new Dictionary<_, _>(1000, HashIdentity.Structural)
10761074
fun x ->
1077-
let mutable res = Unchecked.defaultof<_>
1078-
if t.TryGetValue(x, &res) then
1079-
res
1080-
else
1081-
res <- f x
1075+
match t.TryGetValue x with
1076+
| true, res -> res
1077+
| _ ->
1078+
let res = f x
10821079
t.[x] <- res
10831080
res
10841081

src/absil/ilread.fs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -903,12 +903,11 @@ let mkCacheInt32 lowMem _inbase _nm _sz =
903903
| null -> cache := new Dictionary<int32, _>(11)
904904
| _ -> ()
905905
!cache
906-
let mutable res = Unchecked.defaultof<_>
907-
let ok = cache.TryGetValue(idx, &res)
908-
if ok then
906+
match cache.TryGetValue idx with
907+
| true, res ->
909908
incr count
910909
res
911-
else
910+
| _ ->
912911
let res = f idx
913912
cache.[idx] <- res
914913
res
@@ -4048,7 +4047,7 @@ let OpenILModuleReader fileName opts =
40484047
let cacheResult2 =
40494048
// can't used a cached entry when reading PDBs, since it makes the returned object IDisposable
40504049
if keyOk && opts.pdbDirPath.IsNone then
4051-
ilModuleReaderCache2.TryGetValue(key)
4050+
ilModuleReaderCache2.TryGetValue key
40524051
else
40534052
false, Unchecked.defaultof<_>
40544053

src/absil/ilwrite.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,9 @@ type MetadataTable<'T> =
464464
#if DEBUG
465465
tbl.lookups <- tbl.lookups + 1
466466
#endif
467-
let mutable res = Unchecked.defaultof<_>
468-
let ok = tbl.dict.TryGetValue(x, &res)
469-
if ok then res
470-
else tbl.AddSharedEntry x
467+
match tbl.dict.TryGetValue x with
468+
| true, res -> res
469+
| _ -> tbl.AddSharedEntry x
471470

472471

473472
/// This is only used in one special place - see further below.
@@ -769,11 +768,12 @@ let rec GetTypeRefAsTypeRefRow cenv (tref: ILTypeRef) =
769768
SharedRow [| ResolutionScope (rs1, rs2); nelem; nselem |]
770769

771770
and GetTypeRefAsTypeRefIdx cenv tref =
772-
let mutable res = 0
773-
if cenv.trefCache.TryGetValue(tref, &res) then res else
774-
let res = FindOrAddSharedRow cenv TableNames.TypeRef (GetTypeRefAsTypeRefRow cenv tref)
775-
cenv.trefCache.[tref] <- res
776-
res
771+
match cenv.trefCache.TryGetValue tref with
772+
| true, res -> res
773+
| _ ->
774+
let res = FindOrAddSharedRow cenv TableNames.TypeRef (GetTypeRefAsTypeRefRow cenv tref)
775+
cenv.trefCache.[tref] <- res
776+
res
777777

778778
and GetTypeDescAsTypeRefIdx cenv (scoref, enc, n) =
779779
GetTypeRefAsTypeRefIdx cenv (mkILNestedTyRef (scoref, enc, n))

src/fsharp/AttributeChecking.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ let IsSecurityAttribute (g: TcGlobals) amap (casmap : Dictionary<Stamp, bool>) (
513513
match attr.TyconRef.TryDeref with
514514
| ValueSome _ ->
515515
let tcs = tcref.Stamp
516-
match casmap.TryGetValue(tcs) with
516+
match casmap.TryGetValue tcs with
517517
| true, c -> c
518518
| _ ->
519519
let exists = ExistsInEntireHierarchyOfType (fun t -> typeEquiv g t (mkAppTy attr.TyconRef [])) g amap m AllowMultiIntfInstantiations.Yes (mkAppTy tcref [])

src/fsharp/ExtensionTyping.fs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,18 @@ module internal ExtensionTyping =
336336
match ctxt with
337337
| NoEntries -> None
338338
| Entries(d, _) ->
339-
let mutable res = Unchecked.defaultof<_>
340-
if d.TryGetValue(st, &res) then Some res else None
339+
match d.TryGetValue st with
340+
| true, res -> Some res
341+
| _ -> None
341342

342343
member ctxt.TryGetTyconRef st =
343344
match ctxt with
344345
| NoEntries -> None
345346
| Entries(_, d) ->
346347
let d = d.Force()
347-
let mutable res = Unchecked.defaultof<_>
348-
if d.TryGetValue(st, &res) then Some res else None
348+
match d.TryGetValue st with
349+
| true, res -> Some res
350+
| _ -> None
349351

350352
member ctxt.RemapTyconRefs (f: obj->obj) =
351353
match ctxt with

src/fsharp/InfoReader.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ type PropertyCollector(g, amap, m, ty, optFilter, ad) =
107107
let props = new Dictionary<PropInfo, PropInfo>(hashIdentity)
108108

109109
let add pinfo =
110-
match props.TryGetValue(pinfo), pinfo with
110+
match props.TryGetValue pinfo, pinfo with
111111
| (true, FSProp (_, ty, Some vref1, _)), FSProp (_, _, _, Some vref2)
112112
| (true, FSProp (_, ty, _, Some vref2)), FSProp (_, _, Some vref1, _) ->
113113
let pinfo = FSProp (g, ty, Some vref1, Some vref2)

src/fsharp/MethodCalls.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ let TryImportProvidedMethodBaseAsLibraryIntrinsic (amap: Import.ImportMap, m: ra
725725
match amap.g.knownIntrinsics.TryGetValue ((declaringEntity.LogicalName, methodName)) with
726726
| true, vref -> Some vref
727727
| _ ->
728-
match amap.g.knownFSharpCoreModules.TryGetValue(declaringEntity.LogicalName) with
728+
match amap.g.knownFSharpCoreModules.TryGetValue declaringEntity.LogicalName with
729729
| true, modRef ->
730730
modRef.ModuleOrNamespaceType.AllValsByLogicalName
731731
|> Seq.tryPick (fun (KeyValue(_, v)) -> if v.CompiledName = methodName then Some (mkNestedValRef modRef v) else None)
@@ -1219,7 +1219,7 @@ module ProvidedMethodCalls =
12191219
// sub in the appropriate argument
12201220
// REVIEW: "thisArg" pointer should be first, if present
12211221
let vRaw = pe.PUntaint(id, m)
1222-
match varConv.TryGetValue(vRaw) with
1222+
match varConv.TryGetValue vRaw with
12231223
| true, v -> v
12241224
| _ ->
12251225
let typeProviderDesignation = ExtensionTyping.DisplayNameOfTypeProvider (pe.TypeProvider, m)

src/fsharp/Optimizer.fs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -573,18 +573,18 @@ let GetInfoForLocalValue cenv env (v: Val) m =
573573
// Abstract slots do not have values
574574
if v.IsDispatchSlot then UnknownValInfo
575575
else
576-
let mutable res = Unchecked.defaultof<_>
577-
let ok = cenv.localInternalVals.TryGetValue(v.Stamp, &res)
578-
if ok then res else
579-
match env.localExternalVals.TryFind v.Stamp with
580-
| Some vval -> vval
581-
| None ->
582-
if v.MustInline then
583-
errorR(Error(FSComp.SR.optValueMarkedInlineButWasNotBoundInTheOptEnv(fullDisplayTextOfValRef (mkLocalValRef v)), m))
576+
match cenv.localInternalVals.TryGetValue v.Stamp with
577+
| true, res -> res
578+
| _ ->
579+
match env.localExternalVals.TryFind v.Stamp with
580+
| Some vval -> vval
581+
| None ->
582+
if v.MustInline then
583+
errorR(Error(FSComp.SR.optValueMarkedInlineButWasNotBoundInTheOptEnv(fullDisplayTextOfValRef (mkLocalValRef v)), m))
584584
#if CHECKED
585-
warning(Error(FSComp.SR.optLocalValueNotFoundDuringOptimization(v.DisplayName), m))
585+
warning(Error(FSComp.SR.optLocalValueNotFoundDuringOptimization(v.DisplayName), m))
586586
#endif
587-
UnknownValInfo
587+
UnknownValInfo
588588

589589
let TryGetInfoForCcu env (ccu: CcuThunk) = env.globalModuleInfos.TryFind(ccu.AssemblyName)
590590

0 commit comments

Comments
 (0)