Repository: aws-cloudformation/cfn-lint

Issue:
v1: E1019 Exception 'maximum recursion depth exceeded while calling a Python object' raised while validating 'enum' when 'Ref' is resolved when ... whe 'Ref' is resolved
### CloudFormation Lint Version

v1.3.0

### What operating system are you using?

Ubuntu

### Describe the bug

This is more trick if its a bug, AWS do understand it and does it correct though.

AWS figure out that it inner !Ref should not use the name in !Sub but the rest in the template but as they are the same the Sub named variable is not needed. 
```yaml
                Resource: !Sub
                  - "arn:aws:s3:::${OutputBucketName}"
                  - OutputBucketName: !Ref OutputBucketName
```

the above is the same as this:
```yaml
                Resource: !Sub "arn:aws:s3:::${OutputBucketName}"
```


### Expected behavior

Expected as AWS do accept that structure and it works correct this should be more of a refactoring recommendation but deferentially not be a run away recursion.

### Reproduction template

```yaml
 ---
AWSTemplateFormatVersion: '2010-09-09'
Description: bug E1019 
  IAMRole:
    Type: AWS::IAM::Role
    Properties:
      Description: 'The IAM role'
      Path: '/'
      Policies:
        - PolicyName: 's3-iam-policy'
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Action:
                  - 's3:List*'
                  - 's3:Put*'
                Effect: Allow
                Resource: !Sub
                  - "arn:aws:s3:::${OutputBucketName}"
                  - OutputBucketName: !Ref OutputBucketName
```


Discussion:

Patch:
diff --git a/src/cfnlint/rules/functions/Sub.py b/src/cfnlint/rules/functions/Sub.py
index b3f34b3007..e522ccc7f4 100644
--- a/src/cfnlint/rules/functions/Sub.py
+++ b/src/cfnlint/rules/functions/Sub.py
@@ -10,7 +10,7 @@
 
 import regex as re
 
-from cfnlint.helpers import REGEX_SUB_PARAMETERS
+from cfnlint.helpers import REGEX_SUB_PARAMETERS, is_function
 from cfnlint.jsonschema import ValidationError, ValidationResult, Validator
 from cfnlint.rules.functions._BaseFn import BaseFn
 
@@ -127,8 +127,13 @@ def fn_sub(
             if not validator.is_type(value[1], "object"):
                 return
 
+            sub_values = value[1].copy()
+            for sub_k, sub_v in value[1].items():
+                fn_k, fn_v = is_function(sub_v)
+                if fn_k == "Ref" and fn_v == sub_k:
+                    del sub_values[sub_k]
             validator_string = validator.evolve(
-                context=validator.context.evolve(ref_values=value[1])
+                context=validator.context.evolve(ref_values=sub_values)
             )
             value = value[0]
         elif validator.is_type(value, "string"):


Test patch:
diff --git a/test/unit/rules/functions/test_sub.py b/test/unit/rules/functions/test_sub.py
index 615a287117..2a56a18daf 100644
--- a/test/unit/rules/functions/test_sub.py
+++ b/test/unit/rules/functions/test_sub.py
@@ -137,6 +137,12 @@ def context(cfn):
             {"type": "string"},
             [],
         ),
+        (
+            "Valid Fn::Sub with an ref to resource",
+            {"Fn::Sub": ["${MyResource}", {"MyResource": {"Ref": "MyResource"}}]},
+            {"type": "string"},
+            [],
+        ),
         (
             "Invalid Fn::Sub with a too to many elements",
             {"Fn::Sub": ["${foo}", {"foo": "bar"}, {}]},