Repository: opentofu/opentofu
File: internal/configs/config.go (Go)

Code before:
			constraints, err := getproviders.ParseVersionConstraints(providerReqs.Requirement.Required.String())
			if err != nil {
				diags = diags.Append(&hcl.Diagnostic{
					Severity: hcl.DiagError,
					Summary:  "Invalid version constraint",
					// The errors returned by ParseVersionConstraint already include
					// the section of input that was incorrect, so we don't need to
					// include that here.
					Detail:  fmt.Sprintf("Incorrect version constraint syntax: %s.", err.Error()),
					Subject: providerReqs.Requirement.DeclRange.Ptr(),
				})
			}
			reqs[fqn] = append(reqs[fqn], constraints...)
			qualifs.AddExplicitProvider(providerReqs.Type)
		}
	}

	// Each resource in the configuration creates an *implicit* provider
	// dependency, though we'll only record it if there isn't already
	// an explicit dependency on the same provider.
	for _, rc := range c.Module.ManagedResources {
		fqn := rc.Provider
		if _, exists := reqs[fqn]; exists {
			// If this is called for a child module, and the provider was added from another implicit reference and not
			// from a top level required_provider, we need to collect the reference of this resource as well as implicit provider.
			if _, ok := qualifs.Explicit[fqn]; !ok {
				qualifs.AddImplicitProvider(fqn, rc.DeclRange)
			}
			// Explicit dependency already present
			continue
		}
		qualifs.AddImplicitProvider(fqn, rc.DeclRange)
		reqs[fqn] = nil
	}
	for _, rc := range c.Module.DataResources {
		fqn := rc.Provider
		if _, exists := reqs[fqn]; exists {
			// If this is called for a child module, and the provider was added from another implicit reference and not
			// from a top level required_provider, we need to collect the reference of this resource as well as implicit provider.
			if _, ok := qualifs.Explicit[fqn]; !ok {
				qualifs.AddImplicitProvider(fqn, rc.DeclRange)
			}
			// Explicit dependency already present
			continue
		}
		qualifs.AddImplicitProvider(rc.Provider, rc.DeclRange)
		reqs[fqn] = nil
	}

	// Import blocks that are generating config may also have a custom provider
	// meta argument. Like the provider meta argument used in resource blocks,

Review comment (refactor):
nit: I think we can simplify it a bit and always check explicit providers inside the `AddImplicitProvider` function

Code after:
			// better error messages it produces in more situations.
			constraints, err := getproviders.ParseVersionConstraints(providerReqs.Requirement.Required.String())
			if err != nil {
				diags = diags.Append(&hcl.Diagnostic{
					Severity: hcl.DiagError,
					Summary:  "Invalid version constraint",
					// The errors returned by ParseVersionConstraint already include
					// the section of input that was incorrect, so we don't need to
					// include that here.
					Detail:  fmt.Sprintf("Incorrect version constraint syntax: %s.", err.Error()),
					Subject: providerReqs.Requirement.DeclRange.Ptr(),
				})
			}
			reqs[fqn] = append(reqs[fqn], constraints...)
			qualifs.AddExplicitProvider(providerReqs.Type)
		}
	}

	// Each resource in the configuration creates an *implicit* provider
	// dependency, though we'll only record it if there isn't already
	// an explicit dependency on the same provider.
	for _, rc := range c.Module.ManagedResources {
		fqn := rc.Provider
		if _, exists := reqs[fqn]; exists {
			// If this is called for a child module, and the provider was added from another implicit reference and not
			// from a top level required_provider, we need to collect the reference of this resource as well as implicit provider.
			qualifs.AddImplicitProvider(fqn, getproviders.ResourceRef{
				CfgRes:            rc.Addr().InModule(c.Path),
				Ref:               tfdiags.SourceRangeFromHCL(rc.DeclRange),
				ProviderAttribute: rc.ProviderConfigRef != nil,
			})
			// Explicit dependency already present
			continue
		}
		qualifs.AddImplicitProvider(fqn, getproviders.ResourceRef{
			CfgRes:            rc.Addr().InModule(c.Path),
			Ref:               tfdiags.SourceRangeFromHCL(rc.DeclRange),
			ProviderAttribute: rc.ProviderConfigRef != nil,
		})
		reqs[fqn] = nil
	}
	for _, rc := range c.Module.DataResources {
		fqn := rc.Provider
		if _, exists := reqs[fqn]; exists {
			// If this is called for a child module, and the provider was added from another implicit reference and not
			// from a top level required_provider, we need to collect the reference of this resource as well as implicit provider.
			qualifs.AddImplicitProvider(fqn, getproviders.ResourceRef{
				CfgRes:            rc.Addr().InModule(c.Path),
				Ref:               tfdiags.SourceRangeFromHCL(rc.DeclRange),
				ProviderAttribute: rc.ProviderConfigRef != nil,
			})