Repository: astral-sh/ruff
File: crates/ruff_db/src/diagnostic/render/junit.rs (Rust)

Code before:
            test_suite.add_test_case(case);
            report.add_test_suite(test_suite);
        } else {
            for (filename, diagnostics) in group_diagnostics_by_filename(diagnostics, self.resolver)
            {
                let mut test_suite = TestSuite::new(filename);
                test_suite
                    .extra
                    .insert(XmlString::new("package"), XmlString::new("org.ruff"));

                for diagnostic in diagnostics {
                    let DiagnosticWithLocation {
                        diagnostic,
                        start_location: location,
                    } = diagnostic;

                    let code = diagnostic
                        .secondary_code()
                        .map_or_else(|| diagnostic.name(), SecondaryCode::as_str);
                    let status = TestCaseStatus::non_success(NonSuccessKind::Failure);

                    let mut case = TestCase::new(format!("org.ruff.{code}"), status);
                    let classname = Path::new(filename).with_extension("");
                    case.set_classname(classname.to_str().unwrap_or(filename));
                    let msg = diagnostic.concise_message().to_str();
                    case.status.set_message(msg.clone());

                    let loc = location
                        .and_then(|location| {
                            case.extra.insert(
                                XmlString::new("line"),
                                XmlString::new(location.line.to_string()),
                            );
                            case.extra.insert(
                                XmlString::new("column"),
                                XmlString::new(location.column.to_string()),
                            );
                            Some(format!(
                                "line {row}, col {col}, ",
                                row = location.line,
                                col = location.column,
                            ))
                        })
                        .unwrap_or_default();

                    case.status.set_description(format!("{loc}{msg}"));
                    test_suite.add_test_case(case);
                }
                report.add_test_suite(test_suite);
            }
        }

Review comment (suggestion):
Nit: I've a slight preference to keep `status` mutable as it was before and setting its fields before passing it to `TestCase::new`

Code after:
            case.set_classname(self.program);
            test_suite.add_test_case(case);
            report.add_test_suite(test_suite);
        } else {
            for (filename, diagnostics) in group_diagnostics_by_filename(diagnostics, self.resolver)
            {
                let mut test_suite = TestSuite::new(filename);
                test_suite
                    .extra
                    .insert(XmlString::new("package"), XmlString::new(&package));

                let classname = Path::new(filename).with_extension("");

                for diagnostic in diagnostics {
                    let DiagnosticWithLocation {
                        diagnostic,
                        start_location: location,
                    } = diagnostic;

                    let code = diagnostic
                        .secondary_code()
                        .map_or_else(|| diagnostic.name(), SecondaryCode::as_str);
                    let mut status = TestCaseStatus::non_success(NonSuccessKind::Failure);
                    status.set_message(diagnostic.concise_message().to_str());

                    if let Some(location) = location {
                        status.set_description(format!(
                            "line {row}, col {col}, {body}",
                            row = location.line,
                            col = location.column,
                            body = diagnostic.concise_message()
                        ));
                    } else {
                        status.set_description(diagnostic.concise_message().to_str());
                    }

                    let mut case = TestCase::new(
                        format!("org.{program}.{code}", program = self.program),
                        status,
                    );
                    case.set_classname(classname.to_str().unwrap_or(filename));

                    if let Some(location) = location {
                        case.extra.insert(
                            XmlString::new("line"),
                            XmlString::new(location.line.to_string()),
                        );
                        case.extra.insert(
                            XmlString::new("column"),
                            XmlString::new(location.column.to_string()),
                        );