Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/types/infer/builder.rs (Rust)

Code before:
                                                                slice_ty.display(db),
                                                            )),
                                                    );
                                                }

                                                if !rhs_value_ty
                                                    .is_assignable_to(db, *expected_value_ty)
                                                {
                                                    diagnostic.annotate(
                                                        self.context
                                                            .secondary(rhs_value_node)
                                                            .message(format_args!(
                                                                "Expected value of type `{}`, got `{}`",
                                                                expected_value_ty.display(db),
                                                                rhs_value_ty.display(db),
                                                            )),
                                                    );
                                                }

                                                attach_original_type_info(&mut diagnostic);
                                            } else {
                                                let mut diagnostic = builder.into_diagnostic(format_args!(
                                                    "Invalid subscript assignment with key of type `{}` and value of \
                                                     type `{assigned_d}` on object of type `{object_d}`",
                                                    slice_ty.display(db),
                                                ));
                                                attach_original_type_info(&mut diagnostic);
                                            }
                                        }
                                    }
                                }
                                CallErrorKind::PossiblyNotCallable => {
                                    if emit_diagnostic
                                        && let Some(builder) =
                                            self.context.report_lint(&CALL_NON_CALLABLE, target)
                                    {
                                        let mut diagnostic = builder.into_diagnostic(format_args!(
                                            "Method `__setitem__` of type `{}` may not be callable on object of type `{}`",
                                            bindings.callable_type().display(db),
                                            object_ty.display(db),
                                        ));
                                        attach_original_type_info(&mut diagnostic);
                                    }
                                }
                            }
                            false
                        }
                        CallDunderError::MethodNotAvailable => {
                            if emit_diagnostic
                                && let Some(builder) =
                                    self.context.report_lint(&INVALID_ASSIGNMENT, target)

Review comment (refactor):
you can be a bit more DRY:

```diff
diff --git a/crates/ty_python_semantic/src/types/infer/builder.rs b/crates/ty_python_semantic/src/types/infer/builder.rs
index cfc248d42e..9b12ced6d6 100644
--- a/crates/ty_python_semantic/src/types/infer/builder.rs
+++ b/crates/ty_python_semantic/src/types/infer/builder.rs
@@ -3794,18 +3794,18 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
                                             let assigned_d = rhs_value_ty.display(db);
                                             let object_d = object_ty.display(db);
 
+                                            let mut diagnostic = builder.into_diagnostic(format_args!(
+                                                "Invalid subscript assignment with key of type `{}` and value of \
+                                                    type `{assigned_d}` on object of type `{object_d}`",
+                                                slice_ty.display(db),
+                                            ));
+
                                             // Special diagnostic for dictionaries
                                             if let Some([expected_key_ty, expected_value_ty]) =
                                                 object_ty
                                                     .known_specialization(db, KnownClass::Dict)
                                                     .map(|s| s.types(db))
                                             {
-                                                let mut diagnostic = builder.into_diagnostic(format_args!(
-                                                    "Invalid subscript assignment with key of type `{}` and value of \
-                                                     type `{assigned_d}` on object of type `{object_d}`",
-                                                    slice_ty.display(db),
-                                                ));
-
                                                 if !slice_ty.is_assignable_to(db, *expected_key_ty)
                                                 {
                                                     diagnostic.annotate(
@@ -3833,13 +3833,6 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
                                                     );
                                                 }
 
-                                                attach_original_type_info(&mut diagnostic);
-                                            } else {
-                                                let mut diagnostic = builder.into_diagnostic(format_args!(
-                                                    "Invalid subscript assignment with key of type `{}` and value of \
-                                                     type `{assigned_d}` on object of type `{object_d}`",
-                                                    slice_ty.display(db),
-                                                ));
                                                 attach_original_type_info(&mut diagnostic);
                                             }
                                         }
```

(all tests pass with that patch applied)

Code after:
                                                                slice_ty.display(db),
                                                            )),
                                                    );
                                                }

                                                if !rhs_value_ty
                                                    .is_assignable_to(db, *expected_value_ty)
                                                {
                                                    diagnostic.annotate(
                                                        self.context
                                                            .secondary(rhs_value_node)
                                                            .message(format_args!(
                                                                "Expected value of type `{}`, got `{}`",
                                                                expected_value_ty.display(db),
                                                                rhs_value_ty.display(db),
                                                            )),
                                                    );
                                                }
                                            }

                                            attach_original_type_info(&mut diagnostic);
                                        }
                                    }
                                }
                                CallErrorKind::PossiblyNotCallable => {
                                    if emit_diagnostic
                                        && let Some(builder) =
                                            self.context.report_lint(&CALL_NON_CALLABLE, target)
                                    {
                                        let mut diagnostic = builder.into_diagnostic(format_args!(
                                            "Method `__setitem__` of type `{}` may not be callable on object of type `{}`",
                                            bindings.callable_type().display(db),
                                            object_ty.display(db),
                                        ));
                                        attach_original_type_info(&mut diagnostic);
                                    }
                                }
                            }
                            false
                        }
                        CallDunderError::MethodNotAvailable => {
                            if emit_diagnostic
                                && let Some(builder) =
                                    self.context.report_lint(&INVALID_ASSIGNMENT, target)
                            {
                                let mut diagnostic = builder.into_diagnostic(format_args!(
                                    "Cannot assign to a subscript on an object of type `{}`",
                                    object_ty.display(db),
                                ));
                                attach_original_type_info(&mut diagnostic);