Repository: dotnet/roslyn
File: src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol_ImplementationChecks.cs (C#)

Code before:
            for (int i = 0; i < baseParameters.Length; i++)
            {
                var baseParameter = baseParameters[i];
                var overrideParameter = overrideParameters[i + overrideParameterOffset];
                if (baseParameter.RefKind != overrideParameter.RefKind)
                {
                    reportMismatchInParameterType(diagnostics, baseMethod, overrideMethod, overrideParameter, topLevel: true, (baseParameter, extraArgument));
                }
            }
        }

        internal static void CheckCallerUnsafeMismatch<TArg>(
            Symbol? overriddenMember,
            Symbol? overridingMember,
            ErrorCode errorCode,
            TArg arg,
            Func<TArg, Location> overridingMemberLocation,
            BindingDiagnosticBag diagnostics)
        {
            if (overriddenMember is null || overridingMember is null)
            {
                return;
            }

            overriddenMember = overriddenMember.OriginalDefinition;
            var leastOverriddenMember = overriddenMember.GetLeastOverriddenMember(overriddenMember.ContainingType);
            if (overridingMember.CallerUnsafeMode == CallerUnsafeMode.Explicit && leastOverriddenMember.CallerUnsafeMode == CallerUnsafeMode.None)
            {
                diagnostics.Add(errorCode, overridingMemberLocation(arg), overridingMember, leastOverriddenMember);
            }
        }
#nullable disable

        private static bool PerformValidNullableOverrideCheck(
            CSharpCompilation compilation,
            Symbol overriddenMember,
            Symbol overridingMember)
        {
            // Don't do any validation if the nullable feature is not enabled or
            // the override is not written directly in source
            return overriddenMember is object &&
                   overridingMember is object &&
                   compilation is object &&
                   compilation.IsFeatureEnabled(MessageID.IDS_FeatureNullableReferenceTypes);
        }

        internal static void CheckValidNullableEventOverride<TArg>(
            CSharpCompilation compilation,
            EventSymbol overriddenEvent,
            EventSymbol overridingEvent,
            BindingDiagnosticBag diagnostics,

Review comment (suggestion):
>var leastOverriddenMember = overriddenMember.GetLeastOverriddenMember(overriddenMember.ContainingType); [](http://example.com/codeflow?start=12&length=103)

If we are looking for a least overridden member for an override in source, then I think we should start with that override, not from some intermediate override. Then, we wouldn't make a mistake with the context type. The way it looks to me, to implement the current logic, we don't even need the `overriddenMember` parameter. Then there is also a question whether the check should be performed against the least overridden member, rather than the immediate overridden member, like the other similar methods do. #Closed

Code after:
            for (int i = 0; i < baseParameters.Length; i++)
            {
                var baseParameter = baseParameters[i];
                var overrideParameter = overrideParameters[i + overrideParameterOffset];
                if (baseParameter.RefKind != overrideParameter.RefKind)
                {
                    reportMismatchInParameterType(diagnostics, baseMethod, overrideMethod, overrideParameter, topLevel: true, (baseParameter, extraArgument));
                }
            }
        }

        internal static void CheckCallerUnsafeMismatch<TArg>(
            Symbol? implementedMember,
            Symbol? overridingMember,
            ErrorCode errorCode,
            TArg arg,
            Func<TArg, Location> overridingMemberLocation,
            BindingDiagnosticBag diagnostics)
        {
            if (overridingMember is null)
            {
                return;
            }

            Debug.Assert(implementedMember is not null || overridingMember.IsDefinition);

            var leastOverriddenMember = implementedMember ?? overridingMember.GetLeastOverriddenMember(overridingMember.ContainingType);
            if (overridingMember.CallerUnsafeMode == CallerUnsafeMode.Explicit && leastOverriddenMember.CallerUnsafeMode == CallerUnsafeMode.None)
            {
                diagnostics.Add(errorCode, overridingMemberLocation(arg), overridingMember, leastOverriddenMember);
            }
        }
#nullable disable

        private static bool PerformValidNullableOverrideCheck(
            CSharpCompilation compilation,
            Symbol overriddenMember,
            Symbol overridingMember)
        {
            // Don't do any validation if the nullable feature is not enabled or
            // the override is not written directly in source
            return overriddenMember is object &&
                   overridingMember is object &&
                   compilation is object &&
                   compilation.IsFeatureEnabled(MessageID.IDS_FeatureNullableReferenceTypes);
        }

        internal static void CheckValidNullableEventOverride<TArg>(
            CSharpCompilation compilation,
            EventSymbol overriddenEvent,
            EventSymbol overridingEvent,