Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 95d0d0f

Browse files
committed
Fix tests and check emitting secondary diagnostics
1 parent 65af140 commit 95d0d0f

File tree

1 file changed

+93
-42
lines changed

1 file changed

+93
-42
lines changed

src/actions/diagnostics.rs

Lines changed: 93 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -336,23 +336,31 @@ impl IsWithin for Range {
336336
mod diagnostic_message_test {
337337
use super::*;
338338

339-
pub(super) fn parse_compiler_message(compiler_message: &str) -> ParsedDiagnostics {
339+
pub(super) fn parse_compiler_message(
340+
compiler_message: &str,
341+
with_related_information: bool,
342+
) -> ParsedDiagnostics {
340343
let _ = ::env_logger::try_init();
341344
let cwd = ::std::env::current_dir().unwrap();
342-
parse_diagnostics(compiler_message, &cwd).expect("failed to parse compiler message")
345+
parse_diagnostics(compiler_message, &cwd, with_related_information)
346+
.expect("failed to parse compiler message")
343347
}
344348

345349
pub(super) trait FileDiagnosticTestExt {
350+
fn single_file_results(&self) -> &Vec<(Diagnostic, Vec<Suggestion>)>;
346351
/// Returns (primary message, secondary messages)
347352
fn to_messages(&self) -> Vec<(String, Vec<String>)>;
353+
fn to_primary_messages(&self) -> Vec<String>;
354+
fn to_secondary_messages(&self) -> Vec<String>;
348355
}
349356

350357
impl FileDiagnosticTestExt for ParsedDiagnostics {
358+
fn single_file_results(&self) -> &Vec<(Diagnostic, Vec<Suggestion>)> {
359+
self.diagnostics.values().nth(0).unwrap()
360+
}
361+
351362
fn to_messages(&self) -> Vec<(String, Vec<String>)> {
352-
(self.diagnostics
353-
.values()
354-
.nth(0) // single file results
355-
.unwrap()
363+
(self.single_file_results()
356364
.iter()
357365
.map(|(diagnostic, _)| {
358366
(
@@ -368,6 +376,17 @@ mod diagnostic_message_test {
368376
})
369377
.collect())
370378
}
379+
380+
fn to_primary_messages(&self) -> Vec<String> {
381+
self.to_messages().iter().map(|(p, _)| p.clone()).collect()
382+
}
383+
384+
fn to_secondary_messages(&self) -> Vec<String> {
385+
self.to_messages()
386+
.iter()
387+
.flat_map(|(_, s)| s.clone())
388+
.collect()
389+
}
371390
}
372391

373392
/// ```
@@ -379,9 +398,10 @@ mod diagnostic_message_test {
379398
/// ```
380399
#[test]
381400
fn message_use_after_move() {
382-
let diag = parse_compiler_message(include_str!(
383-
"../../test_data/compiler_message/use-after-move.json"
384-
));
401+
let diag = parse_compiler_message(
402+
include_str!("../../test_data/compiler_message/use-after-move.json"),
403+
true,
404+
);
385405

386406
let diagnostic = &diag.diagnostics.values().nth(0).unwrap()[0];
387407

@@ -408,9 +428,10 @@ mod diagnostic_message_test {
408428
/// ```
409429
#[test]
410430
fn message_type_annotations_needed() {
411-
let messages = parse_compiler_message(include_str!(
412-
"../../test_data/compiler_message/type-annotations-needed.json"
413-
)).to_messages();
431+
let messages = parse_compiler_message(
432+
include_str!("../../test_data/compiler_message/type-annotations-needed.json"),
433+
true,
434+
).to_messages();
414435
assert_eq!(
415436
messages[0].0,
416437
"type annotations needed\n\n\
@@ -421,6 +442,26 @@ mod diagnostic_message_test {
421442
messages[0].1,
422443
vec!["consider giving `v` a type", "cannot infer type for `T`"]
423444
);
445+
446+
// Check if we don't emit related information if it's not supported and
447+
// if secondary spans are emitted as separate diagnostics
448+
let messages = parse_compiler_message(
449+
include_str!("../../test_data/compiler_message/type-annotations-needed.json"),
450+
false,
451+
);
452+
453+
assert_eq!(
454+
messages.to_primary_messages(),
455+
vec![
456+
"type annotations needed\n\n\
457+
cannot infer type for `T`",
458+
"type annotations needed\n\n\
459+
consider giving `v` a type",
460+
]
461+
);
462+
463+
let secondaries = messages.to_secondary_messages();
464+
assert!(secondaries.is_empty(), "{:?}", secondaries);
424465
}
425466

426467
/// ```
@@ -430,9 +471,10 @@ mod diagnostic_message_test {
430471
/// ```
431472
#[test]
432473
fn message_mismatched_types() {
433-
let messages = parse_compiler_message(include_str!(
434-
"../../test_data/compiler_message/mismatched-types.json"
435-
)).to_messages();
474+
let messages = parse_compiler_message(
475+
include_str!("../../test_data/compiler_message/mismatched-types.json"),
476+
true,
477+
).to_messages();
436478
assert_eq!(
437479
messages[0].0,
438480
"mismatched types\n\n\
@@ -456,9 +498,10 @@ mod diagnostic_message_test {
456498
/// ```
457499
#[test]
458500
fn message_not_mutable() {
459-
let messages = parse_compiler_message(include_str!(
460-
"../../test_data/compiler_message/not-mut.json"
461-
)).to_messages();
501+
let messages = parse_compiler_message(
502+
include_str!("../../test_data/compiler_message/not-mut.json"),
503+
true,
504+
).to_messages();
462505
assert_eq!(
463506
messages[0].0,
464507
"cannot borrow immutable local variable `string` as mutable\n\n\
@@ -484,9 +527,10 @@ mod diagnostic_message_test {
484527
/// ```
485528
#[test]
486529
fn message_consider_borrowing() {
487-
let messages = parse_compiler_message(include_str!(
488-
"../../test_data/compiler_message/consider-borrowing.json"
489-
)).to_messages();
530+
let messages = parse_compiler_message(
531+
include_str!("../../test_data/compiler_message/consider-borrowing.json"),
532+
true,
533+
).to_messages();
490534
assert_eq!(
491535
messages[0].0,
492536
r#"mismatched types
@@ -514,9 +558,10 @@ help: consider borrowing here: `&string`"#,
514558
/// ```
515559
#[test]
516560
fn message_move_out_of_borrow() {
517-
let messages = parse_compiler_message(include_str!(
518-
"../../test_data/compiler_message/move-out-of-borrow.json"
519-
)).to_messages();
561+
let messages = parse_compiler_message(
562+
include_str!("../../test_data/compiler_message/move-out-of-borrow.json"),
563+
true,
564+
).to_messages();
520565
assert_eq!(
521566
messages[0].0,
522567
"cannot move out of borrowed content\n\ncannot move out of borrowed content"
@@ -536,9 +581,10 @@ help: consider borrowing here: `&string`"#,
536581
/// ```
537582
#[test]
538583
fn message_unused_use() {
539-
let messages = parse_compiler_message(include_str!(
540-
"../../test_data/compiler_message/unused-use.json"
541-
)).to_messages();
584+
let messages = parse_compiler_message(
585+
include_str!("../../test_data/compiler_message/unused-use.json"),
586+
true,
587+
).to_messages();
542588

543589
// Single compiler message with 3 primary spans should emit 3 separate
544590
// diagnostics.
@@ -555,9 +601,10 @@ help: consider borrowing here: `&string`"#,
555601

556602
#[test]
557603
fn message_cannot_find_type() {
558-
let messages = parse_compiler_message(include_str!(
559-
"../../test_data/compiler_message/cannot-find-type.json"
560-
)).to_messages();
604+
let messages = parse_compiler_message(
605+
include_str!("../../test_data/compiler_message/cannot-find-type.json"),
606+
true,
607+
).to_messages();
561608
assert_eq!(
562609
messages[0].0,
563610
"cannot find type `HashSet` in this scope\n\n\
@@ -572,9 +619,10 @@ help: consider borrowing here: `&string`"#,
572619
/// ```
573620
#[test]
574621
fn message_clippy_identity_op() {
575-
let diag = parse_compiler_message(include_str!(
576-
"../../test_data/compiler_message/clippy-identity-op.json"
577-
));
622+
let diag = parse_compiler_message(
623+
include_str!("../../test_data/compiler_message/clippy-identity-op.json"),
624+
true,
625+
);
578626

579627
let diagnostic = &diag.diagnostics.values().nth(0).unwrap()[0];
580628

@@ -612,9 +660,10 @@ mod diagnostic_suggestion_test {
612660

613661
#[test]
614662
fn suggest_use_when_cannot_find_type() {
615-
let diag = parse_compiler_message(include_str!(
616-
"../../test_data/compiler_message/cannot-find-type.json"
617-
));
663+
let diag = parse_compiler_message(
664+
include_str!("../../test_data/compiler_message/cannot-find-type.json"),
665+
true,
666+
);
618667

619668
let diagnostics = diag.diagnostics.values().nth(0).unwrap();
620669

@@ -646,9 +695,10 @@ mod diagnostic_suggestion_test {
646695

647696
#[test]
648697
fn suggest_mut_when_not_mut() {
649-
let diag = parse_compiler_message(include_str!(
650-
"../../test_data/compiler_message/not-mut.json"
651-
));
698+
let diag = parse_compiler_message(
699+
include_str!("../../test_data/compiler_message/not-mut.json"),
700+
true,
701+
);
652702

653703
let diagnostics = diag.diagnostics.values().nth(0).unwrap();
654704

@@ -682,9 +732,10 @@ mod diagnostic_suggestion_test {
682732
/// ```
683733
#[test]
684734
fn suggest_clippy_const_static() {
685-
let diag = parse_compiler_message(include_str!(
686-
"../../test_data/compiler_message/clippy-const-static-lifetime.json"
687-
));
735+
let diag = parse_compiler_message(
736+
include_str!("../../test_data/compiler_message/clippy-const-static-lifetime.json"),
737+
true,
738+
);
688739

689740
let diagnostics = diag.diagnostics.values().nth(0).unwrap();
690741

0 commit comments

Comments
 (0)