2020-04-17 11:06:02 +00:00
|
|
|
//! Collects diagnostics & fixits for a single file.
|
|
|
|
//!
|
|
|
|
//! The tricky bit here is that diagnostics are produced by hir in terms of
|
|
|
|
//! macro-expanded files, but we need to present them to the users in terms of
|
|
|
|
//! original files. So we need to map the ranges.
|
2019-09-30 08:58:53 +00:00
|
|
|
|
2019-03-24 07:21:36 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
|
2020-08-11 14:13:40 +00:00
|
|
|
use hir::{diagnostics::DiagnosticSinkBuilder, Semantics};
|
2019-03-21 16:05:15 +00:00
|
|
|
use itertools::Itertools;
|
2020-08-10 21:55:57 +00:00
|
|
|
use ra_db::SourceDatabase;
|
2020-02-06 11:52:32 +00:00
|
|
|
use ra_ide_db::RootDatabase;
|
2019-07-04 20:05:17 +00:00
|
|
|
use ra_prof::profile;
|
2019-03-21 16:05:15 +00:00
|
|
|
use ra_syntax::{
|
2020-08-10 21:55:57 +00:00
|
|
|
ast::{self, AstNode},
|
2020-02-06 00:33:18 +00:00
|
|
|
SyntaxNode, TextRange, T,
|
2019-03-21 16:05:15 +00:00
|
|
|
};
|
|
|
|
use ra_text_edit::{TextEdit, TextEditBuilder};
|
|
|
|
|
2020-08-10 21:55:57 +00:00
|
|
|
use crate::{Diagnostic, FileId, Fix, SourceFileEdit};
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-08-10 21:37:23 +00:00
|
|
|
mod diagnostics_with_fix;
|
|
|
|
use diagnostics_with_fix::DiagnosticWithFix;
|
|
|
|
|
2019-03-23 16:34:49 +00:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum Severity {
|
|
|
|
Error,
|
|
|
|
WeakWarning,
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:39:44 +00:00
|
|
|
pub(crate) fn diagnostics(
|
|
|
|
db: &RootDatabase,
|
|
|
|
file_id: FileId,
|
|
|
|
enable_experimental: bool,
|
|
|
|
) -> Vec<Diagnostic> {
|
2019-05-05 14:32:53 +00:00
|
|
|
let _p = profile("diagnostics");
|
2020-02-18 17:35:10 +00:00
|
|
|
let sema = Semantics::new(db);
|
2019-05-28 15:46:11 +00:00
|
|
|
let parse = db.parse(file_id);
|
2019-03-21 16:21:00 +00:00
|
|
|
let mut res = Vec::new();
|
|
|
|
|
2020-07-15 14:05:45 +00:00
|
|
|
// [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily.
|
|
|
|
res.extend(parse.errors().iter().take(128).map(|err| Diagnostic {
|
2020-02-10 00:08:49 +00:00
|
|
|
range: err.range(),
|
2019-05-28 15:46:11 +00:00
|
|
|
message: format!("Syntax Error: {}", err),
|
|
|
|
severity: Severity::Error,
|
|
|
|
fix: None,
|
|
|
|
}));
|
|
|
|
|
2019-07-12 16:41:13 +00:00
|
|
|
for node in parse.tree().syntax().descendants() {
|
2019-07-19 09:56:47 +00:00
|
|
|
check_unnecessary_braces_in_use_statement(&mut res, file_id, &node);
|
|
|
|
check_struct_shorthand_initialization(&mut res, file_id, &node);
|
2019-03-21 16:21:00 +00:00
|
|
|
}
|
2019-03-24 07:21:36 +00:00
|
|
|
let res = RefCell::new(res);
|
2020-07-24 14:30:12 +00:00
|
|
|
let mut sink = DiagnosticSinkBuilder::new()
|
|
|
|
.on::<hir::diagnostics::UnresolvedModule, _>(|d| {
|
2020-08-10 21:55:57 +00:00
|
|
|
res.borrow_mut().push(diagnostic_with_fix(d, &sema));
|
2019-03-24 07:21:36 +00:00
|
|
|
})
|
2020-07-24 14:30:12 +00:00
|
|
|
.on::<hir::diagnostics::MissingFields, _>(|d| {
|
2020-08-10 21:55:57 +00:00
|
|
|
res.borrow_mut().push(diagnostic_with_fix(d, &sema));
|
2019-04-10 21:00:56 +00:00
|
|
|
})
|
2020-07-24 14:30:12 +00:00
|
|
|
.on::<hir::diagnostics::MissingOkInTailExpr, _>(|d| {
|
2020-08-10 21:55:57 +00:00
|
|
|
res.borrow_mut().push(diagnostic_with_fix(d, &sema));
|
2019-08-10 15:40:48 +00:00
|
|
|
})
|
2020-07-24 14:30:12 +00:00
|
|
|
.on::<hir::diagnostics::NoSuchField, _>(|d| {
|
2020-08-10 21:55:57 +00:00
|
|
|
res.borrow_mut().push(diagnostic_with_fix(d, &sema));
|
2020-06-09 21:11:16 +00:00
|
|
|
})
|
2020-07-24 15:39:44 +00:00
|
|
|
// Only collect experimental diagnostics when they're enabled.
|
|
|
|
.filter(|diag| !diag.is_experimental() || enable_experimental)
|
|
|
|
// Diagnostics not handled above get no fix and default treatment.
|
2020-07-24 14:30:12 +00:00
|
|
|
.build(|d| {
|
|
|
|
res.borrow_mut().push(Diagnostic {
|
|
|
|
message: d.message(),
|
2020-08-11 14:13:40 +00:00
|
|
|
range: sema.diagnostics_display_range(d).range,
|
2020-07-24 14:30:12 +00:00
|
|
|
severity: Severity::Error,
|
|
|
|
fix: None,
|
|
|
|
})
|
|
|
|
});
|
2020-06-09 21:11:16 +00:00
|
|
|
|
2020-02-18 17:35:10 +00:00
|
|
|
if let Some(m) = sema.to_module_def(file_id) {
|
2019-03-24 07:21:36 +00:00
|
|
|
m.diagnostics(db, &mut sink);
|
2019-02-08 11:30:21 +00:00
|
|
|
};
|
2019-03-24 07:21:36 +00:00
|
|
|
drop(sink);
|
|
|
|
res.into_inner()
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
2019-03-21 16:05:15 +00:00
|
|
|
|
2020-08-11 14:13:40 +00:00
|
|
|
fn diagnostic_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic {
|
2020-08-10 21:55:57 +00:00
|
|
|
Diagnostic {
|
2020-08-11 14:13:40 +00:00
|
|
|
range: sema.diagnostics_display_range(d).range,
|
2020-08-10 21:55:57 +00:00
|
|
|
message: d.message(),
|
|
|
|
severity: Severity::Error,
|
|
|
|
fix: d.fix(&sema),
|
2020-06-09 21:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 16:05:15 +00:00
|
|
|
fn check_unnecessary_braces_in_use_statement(
|
|
|
|
acc: &mut Vec<Diagnostic>,
|
2019-03-21 16:21:00 +00:00
|
|
|
file_id: FileId,
|
2019-03-21 16:05:15 +00:00
|
|
|
node: &SyntaxNode,
|
|
|
|
) -> Option<()> {
|
2019-07-19 09:56:47 +00:00
|
|
|
let use_tree_list = ast::UseTreeList::cast(node.clone())?;
|
2019-03-21 16:05:15 +00:00
|
|
|
if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() {
|
2020-07-27 12:53:57 +00:00
|
|
|
let use_range = use_tree_list.syntax().text_range();
|
2019-03-21 16:05:15 +00:00
|
|
|
let edit =
|
2019-07-19 09:56:47 +00:00
|
|
|
text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(&single_use_tree)
|
2019-03-21 16:05:15 +00:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
let to_replace = single_use_tree.syntax().text().to_string();
|
|
|
|
let mut edit_builder = TextEditBuilder::default();
|
2020-07-27 12:53:57 +00:00
|
|
|
edit_builder.delete(use_range);
|
|
|
|
edit_builder.insert(use_range.start(), to_replace);
|
2019-03-21 16:05:15 +00:00
|
|
|
edit_builder.finish()
|
|
|
|
});
|
|
|
|
|
|
|
|
acc.push(Diagnostic {
|
2020-07-27 12:53:57 +00:00
|
|
|
range: use_range,
|
2019-06-04 06:29:50 +00:00
|
|
|
message: "Unnecessary braces in use statement".to_string(),
|
2019-03-21 16:05:15 +00:00
|
|
|
severity: Severity::WeakWarning,
|
2020-08-11 14:13:40 +00:00
|
|
|
fix: Some(Fix::new(
|
|
|
|
"Remove unnecessary braces",
|
|
|
|
SourceFileEdit { file_id, edit }.into(),
|
2020-07-27 12:53:57 +00:00
|
|
|
use_range,
|
2019-03-24 20:53:41 +00:00
|
|
|
)),
|
2019-03-21 16:05:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
|
|
|
|
single_use_tree: &ast::UseTree,
|
|
|
|
) -> Option<TextEdit> {
|
|
|
|
let use_tree_list_node = single_use_tree.syntax().parent()?;
|
2019-05-15 12:35:47 +00:00
|
|
|
if single_use_tree.path()?.segment()?.syntax().first_child_or_token()?.kind() == T![self] {
|
2019-07-20 09:58:27 +00:00
|
|
|
let start = use_tree_list_node.prev_sibling_or_token()?.text_range().start();
|
|
|
|
let end = use_tree_list_node.text_range().end();
|
2020-07-27 12:53:57 +00:00
|
|
|
return Some(TextEdit::delete(TextRange::new(start, end)));
|
2019-03-21 16:05:15 +00:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_struct_shorthand_initialization(
|
|
|
|
acc: &mut Vec<Diagnostic>,
|
2019-03-21 16:21:00 +00:00
|
|
|
file_id: FileId,
|
2019-03-21 16:05:15 +00:00
|
|
|
node: &SyntaxNode,
|
|
|
|
) -> Option<()> {
|
2020-07-30 14:21:30 +00:00
|
|
|
let record_lit = ast::RecordExpr::cast(node.clone())?;
|
|
|
|
let record_field_list = record_lit.record_expr_field_list()?;
|
2019-08-23 12:55:21 +00:00
|
|
|
for record_field in record_field_list.fields() {
|
|
|
|
if let (Some(name_ref), Some(expr)) = (record_field.name_ref(), record_field.expr()) {
|
2019-03-21 16:05:15 +00:00
|
|
|
let field_name = name_ref.syntax().text().to_string();
|
|
|
|
let field_expr = expr.syntax().text().to_string();
|
2020-06-12 14:16:19 +00:00
|
|
|
let field_name_is_tup_index = name_ref.as_tuple_field().is_some();
|
2020-06-12 05:11:54 +00:00
|
|
|
if field_name == field_expr && !field_name_is_tup_index {
|
2019-03-21 16:05:15 +00:00
|
|
|
let mut edit_builder = TextEditBuilder::default();
|
2019-08-23 12:55:21 +00:00
|
|
|
edit_builder.delete(record_field.syntax().text_range());
|
|
|
|
edit_builder.insert(record_field.syntax().text_range().start(), field_name);
|
2019-03-21 16:05:15 +00:00
|
|
|
let edit = edit_builder.finish();
|
|
|
|
|
2020-07-27 12:53:57 +00:00
|
|
|
let field_range = record_field.syntax().text_range();
|
2019-03-21 16:05:15 +00:00
|
|
|
acc.push(Diagnostic {
|
2020-07-27 12:53:57 +00:00
|
|
|
range: field_range,
|
2019-06-04 06:29:50 +00:00
|
|
|
message: "Shorthand struct initialization".to_string(),
|
2019-03-21 16:05:15 +00:00
|
|
|
severity: Severity::WeakWarning,
|
2020-08-11 14:13:40 +00:00
|
|
|
fix: Some(Fix::new(
|
|
|
|
"Use struct shorthand initialization",
|
|
|
|
SourceFileEdit { file_id, edit }.into(),
|
2020-07-27 12:53:57 +00:00
|
|
|
field_range,
|
2019-03-24 20:53:41 +00:00
|
|
|
)),
|
2019-03-21 16:05:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-06-23 20:27:24 +00:00
|
|
|
use stdx::trim_indent;
|
2019-07-04 20:05:17 +00:00
|
|
|
use test_utils::assert_eq_text;
|
2019-03-25 11:28:04 +00:00
|
|
|
|
2020-07-09 12:33:03 +00:00
|
|
|
use crate::mock_analysis::{analysis_and_position, single_file, MockAnalysis};
|
|
|
|
use expect::{expect, Expect};
|
2019-03-21 16:05:15 +00:00
|
|
|
|
2019-08-17 06:12:50 +00:00
|
|
|
/// Takes a multi-file input fixture with annotated cursor positions,
|
|
|
|
/// and checks that:
|
|
|
|
/// * a diagnostic is produced
|
2020-07-27 12:53:57 +00:00
|
|
|
/// * this diagnostic fix touches the input cursor position
|
2019-08-17 06:12:50 +00:00
|
|
|
/// * that the contents of the file containing the cursor match `after` after the diagnostic fix is applied
|
2020-07-09 11:59:49 +00:00
|
|
|
fn check_fix(ra_fixture_before: &str, ra_fixture_after: &str) {
|
|
|
|
let after = trim_indent(ra_fixture_after);
|
2020-06-23 20:27:24 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
let (analysis, file_position) = analysis_and_position(ra_fixture_before);
|
2020-07-24 15:39:44 +00:00
|
|
|
let diagnostic = analysis.diagnostics(file_position.file_id, true).unwrap().pop().unwrap();
|
2020-08-11 14:13:40 +00:00
|
|
|
let mut fix = diagnostic.fix.unwrap();
|
2020-05-22 16:03:08 +00:00
|
|
|
let edit = fix.source_change.source_file_edits.pop().unwrap().edit;
|
2019-08-17 06:12:50 +00:00
|
|
|
let target_file_contents = analysis.file_text(file_position.file_id).unwrap();
|
2020-05-05 21:48:26 +00:00
|
|
|
let actual = {
|
|
|
|
let mut actual = target_file_contents.to_string();
|
|
|
|
edit.apply(&mut actual);
|
|
|
|
actual
|
|
|
|
};
|
2019-08-17 06:37:37 +00:00
|
|
|
|
|
|
|
assert_eq_text!(&after, &actual);
|
2019-08-17 06:12:50 +00:00
|
|
|
assert!(
|
2020-08-11 14:13:40 +00:00
|
|
|
fix.fix_trigger_range.start() <= file_position.offset
|
|
|
|
&& fix.fix_trigger_range.end() >= file_position.offset,
|
2020-07-27 12:53:57 +00:00
|
|
|
"diagnostic fix range {:?} does not touch cursor position {:?}",
|
2020-08-11 14:13:40 +00:00
|
|
|
fix.fix_trigger_range,
|
2019-08-17 06:12:50 +00:00
|
|
|
file_position.offset
|
|
|
|
);
|
2019-08-11 12:25:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 19:29:11 +00:00
|
|
|
/// Checks that a diagnostic applies to the file containing the `<|>` cursor marker
|
|
|
|
/// which has a fix that can apply to other files.
|
|
|
|
fn check_apply_diagnostic_fix_in_other_file(ra_fixture_before: &str, ra_fixture_after: &str) {
|
|
|
|
let ra_fixture_after = &trim_indent(ra_fixture_after);
|
|
|
|
let (analysis, file_pos) = analysis_and_position(ra_fixture_before);
|
|
|
|
let current_file_id = file_pos.file_id;
|
2020-07-24 15:39:44 +00:00
|
|
|
let diagnostic = analysis.diagnostics(current_file_id, true).unwrap().pop().unwrap();
|
2020-08-11 14:13:40 +00:00
|
|
|
let mut fix = diagnostic.fix.unwrap();
|
2020-06-22 19:29:11 +00:00
|
|
|
let edit = fix.source_change.source_file_edits.pop().unwrap();
|
|
|
|
let changed_file_id = edit.file_id;
|
|
|
|
let before = analysis.file_text(changed_file_id).unwrap();
|
|
|
|
let actual = {
|
|
|
|
let mut actual = before.to_string();
|
|
|
|
edit.edit.apply(&mut actual);
|
|
|
|
actual
|
|
|
|
};
|
|
|
|
assert_eq_text!(ra_fixture_after, &actual);
|
|
|
|
}
|
|
|
|
|
2019-08-17 06:12:50 +00:00
|
|
|
/// Takes a multi-file input fixture with annotated cursor position and checks that no diagnostics
|
|
|
|
/// apply to the file containing the cursor.
|
2020-07-09 12:33:03 +00:00
|
|
|
fn check_no_diagnostics(ra_fixture: &str) {
|
|
|
|
let mock = MockAnalysis::with_files(ra_fixture);
|
|
|
|
let files = mock.files().map(|(it, _)| it).collect::<Vec<_>>();
|
|
|
|
let analysis = mock.analysis();
|
|
|
|
let diagnostics = files
|
|
|
|
.into_iter()
|
2020-07-24 15:39:44 +00:00
|
|
|
.flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap())
|
2020-07-09 12:33:03 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expect(ra_fixture: &str, expect: Expect) {
|
2020-07-01 15:08:45 +00:00
|
|
|
let (analysis, file_id) = single_file(ra_fixture);
|
2020-07-24 15:39:44 +00:00
|
|
|
let diagnostics = analysis.diagnostics(file_id, true).unwrap();
|
2020-07-09 12:33:03 +00:00
|
|
|
expect.assert_debug_eq(&diagnostics)
|
2019-04-10 21:00:56 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 15:40:48 +00:00
|
|
|
#[test]
|
|
|
|
fn test_wrap_return_type() {
|
2020-07-09 11:59:49 +00:00
|
|
|
check_fix(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
use core::result::Result::{self, Ok, Err};
|
2019-08-10 15:40:48 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
fn div(x: i32, y: i32) -> Result<i32, ()> {
|
|
|
|
if y == 0 {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
x / y<|>
|
|
|
|
}
|
|
|
|
//- /core/lib.rs
|
|
|
|
pub mod result {
|
|
|
|
pub enum Result<T, E> { Ok(T), Err(E) }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use core::result::Result::{self, Ok, Err};
|
2019-08-17 06:37:37 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
fn div(x: i32, y: i32) -> Result<i32, ()> {
|
|
|
|
if y == 0 {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
Ok(x / y)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2019-08-10 15:40:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 14:00:37 +00:00
|
|
|
#[test]
|
|
|
|
fn test_wrap_return_type_handles_generic_functions() {
|
2020-07-09 11:59:49 +00:00
|
|
|
check_fix(
|
|
|
|
r#"
|
2020-07-09 12:33:03 +00:00
|
|
|
//- /main.rs
|
2020-07-09 11:59:49 +00:00
|
|
|
use core::result::Result::{self, Ok, Err};
|
2019-08-11 14:00:37 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
fn div<T>(x: T) -> Result<T, i32> {
|
|
|
|
if x == 0 {
|
|
|
|
return Err(7);
|
|
|
|
}
|
|
|
|
<|>x
|
|
|
|
}
|
|
|
|
//- /core/lib.rs
|
|
|
|
pub mod result {
|
|
|
|
pub enum Result<T, E> { Ok(T), Err(E) }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use core::result::Result::{self, Ok, Err};
|
2019-08-17 06:37:37 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
fn div<T>(x: T) -> Result<T, i32> {
|
|
|
|
if x == 0 {
|
|
|
|
return Err(7);
|
|
|
|
}
|
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2019-08-11 14:00:37 +00:00
|
|
|
}
|
|
|
|
|
2019-08-11 12:47:33 +00:00
|
|
|
#[test]
|
|
|
|
fn test_wrap_return_type_handles_type_aliases() {
|
2020-07-09 11:59:49 +00:00
|
|
|
check_fix(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
use core::result::Result::{self, Ok, Err};
|
2019-08-11 12:47:33 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
type MyResult<T> = Result<T, ()>;
|
2019-08-11 12:47:33 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
fn div(x: i32, y: i32) -> MyResult<i32> {
|
|
|
|
if y == 0 {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
x <|>/ y
|
|
|
|
}
|
|
|
|
//- /core/lib.rs
|
|
|
|
pub mod result {
|
|
|
|
pub enum Result<T, E> { Ok(T), Err(E) }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use core::result::Result::{self, Ok, Err};
|
2019-08-17 06:37:37 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
type MyResult<T> = Result<T, ()>;
|
2020-06-23 20:27:24 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
fn div(x: i32, y: i32) -> MyResult<i32> {
|
|
|
|
if y == 0 {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
Ok(x / y)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2019-08-11 12:47:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 15:40:48 +00:00
|
|
|
#[test]
|
2019-08-17 05:31:53 +00:00
|
|
|
fn test_wrap_return_type_not_applicable_when_expr_type_does_not_match_ok_type() {
|
2020-07-09 12:33:03 +00:00
|
|
|
check_no_diagnostics(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
use core::result::Result::{self, Ok, Err};
|
2019-08-11 12:25:36 +00:00
|
|
|
|
2020-07-09 12:33:03 +00:00
|
|
|
fn foo() -> Result<(), i32> { 0 }
|
2019-08-11 12:25:36 +00:00
|
|
|
|
2020-07-09 12:33:03 +00:00
|
|
|
//- /core/lib.rs
|
|
|
|
pub mod result {
|
|
|
|
pub enum Result<T, E> { Ok(T), Err(E) }
|
|
|
|
}
|
|
|
|
"#,
|
2020-07-01 15:08:45 +00:00
|
|
|
);
|
2019-08-10 15:40:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 05:31:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_wrap_return_type_not_applicable_when_return_type_is_not_result() {
|
2020-07-09 12:33:03 +00:00
|
|
|
check_no_diagnostics(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
use core::result::Result::{self, Ok, Err};
|
2019-08-17 05:31:53 +00:00
|
|
|
|
2020-07-09 12:33:03 +00:00
|
|
|
enum SomeOtherEnum { Ok(i32), Err(String) }
|
2019-08-17 05:31:53 +00:00
|
|
|
|
2020-07-09 12:33:03 +00:00
|
|
|
fn foo() -> SomeOtherEnum { 0 }
|
2019-08-17 05:31:53 +00:00
|
|
|
|
2020-07-09 12:33:03 +00:00
|
|
|
//- /core/lib.rs
|
|
|
|
pub mod result {
|
|
|
|
pub enum Result<T, E> { Ok(T), Err(E) }
|
|
|
|
}
|
|
|
|
"#,
|
2020-07-01 15:08:45 +00:00
|
|
|
);
|
2019-08-17 05:31:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 21:00:56 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fill_struct_fields_empty() {
|
2020-07-09 11:59:49 +00:00
|
|
|
check_fix(
|
|
|
|
r#"
|
|
|
|
struct TestStruct { one: i32, two: i64 }
|
2019-04-10 21:00:56 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
fn test_fn() {
|
|
|
|
let s = TestStruct {<|>};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct TestStruct { one: i32, two: i64 }
|
2019-04-10 21:00:56 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
fn test_fn() {
|
|
|
|
let s = TestStruct { one: (), two: ()};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-02-19 16:53:32 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 14:26:49 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fill_struct_fields_self() {
|
2020-07-09 11:59:49 +00:00
|
|
|
check_fix(
|
|
|
|
r#"
|
|
|
|
struct TestStruct { one: i32 }
|
2020-03-06 14:26:49 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
impl TestStruct {
|
|
|
|
fn test_fn() { let s = Self {<|>}; }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct TestStruct { one: i32 }
|
2020-03-06 14:26:49 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
impl TestStruct {
|
|
|
|
fn test_fn() { let s = Self { one: ()}; }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-03-06 14:26:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 16:53:32 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fill_struct_fields_enum() {
|
2020-07-09 11:59:49 +00:00
|
|
|
check_fix(
|
|
|
|
r#"
|
|
|
|
enum Expr {
|
|
|
|
Bin { lhs: Box<Expr>, rhs: Box<Expr> }
|
|
|
|
}
|
2020-02-19 16:53:32 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
impl Expr {
|
|
|
|
fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
|
|
|
|
Expr::Bin {<|> }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
enum Expr {
|
|
|
|
Bin { lhs: Box<Expr>, rhs: Box<Expr> }
|
|
|
|
}
|
2020-02-19 16:53:32 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
impl Expr {
|
|
|
|
fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
|
|
|
|
Expr::Bin { lhs: (), rhs: () }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2019-04-10 21:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fill_struct_fields_partial() {
|
2020-07-09 11:59:49 +00:00
|
|
|
check_fix(
|
|
|
|
r#"
|
|
|
|
struct TestStruct { one: i32, two: i64 }
|
2019-04-10 21:00:56 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
fn test_fn() {
|
|
|
|
let s = TestStruct{ two: 2<|> };
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r"
|
|
|
|
struct TestStruct { one: i32, two: i64 }
|
2019-04-10 21:00:56 +00:00
|
|
|
|
2020-07-09 11:59:49 +00:00
|
|
|
fn test_fn() {
|
|
|
|
let s = TestStruct{ two: 2, one: () };
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
2019-04-10 21:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fill_struct_fields_no_diagnostic() {
|
2020-07-09 12:33:03 +00:00
|
|
|
check_no_diagnostics(
|
2020-07-01 15:08:45 +00:00
|
|
|
r"
|
2020-07-09 11:59:49 +00:00
|
|
|
struct TestStruct { one: i32, two: i64 }
|
2019-05-13 16:39:06 +00:00
|
|
|
|
2019-04-10 21:00:56 +00:00
|
|
|
fn test_fn() {
|
|
|
|
let one = 1;
|
|
|
|
let s = TestStruct{ one, two: 2 };
|
|
|
|
}
|
2020-07-01 15:08:45 +00:00
|
|
|
",
|
|
|
|
);
|
2019-04-10 21:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fill_struct_fields_no_diagnostic_on_spread() {
|
2020-07-09 12:33:03 +00:00
|
|
|
check_no_diagnostics(
|
2020-07-01 15:08:45 +00:00
|
|
|
r"
|
2020-07-09 11:59:49 +00:00
|
|
|
struct TestStruct { one: i32, two: i64 }
|
2019-05-13 16:39:06 +00:00
|
|
|
|
2019-04-10 21:00:56 +00:00
|
|
|
fn test_fn() {
|
|
|
|
let one = 1;
|
|
|
|
let s = TestStruct{ ..a };
|
|
|
|
}
|
2020-07-01 15:08:45 +00:00
|
|
|
",
|
|
|
|
);
|
2019-04-10 21:00:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 11:28:04 +00:00
|
|
|
#[test]
|
|
|
|
fn test_unresolved_module_diagnostic() {
|
2020-07-09 12:33:03 +00:00
|
|
|
check_expect(
|
|
|
|
r#"mod foo;"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
Diagnostic {
|
|
|
|
message: "unresolved module",
|
|
|
|
range: 0..8,
|
|
|
|
severity: Error,
|
|
|
|
fix: Some(
|
2020-08-11 14:13:40 +00:00
|
|
|
Fix {
|
|
|
|
label: "Create module",
|
|
|
|
source_change: SourceChange {
|
|
|
|
source_file_edits: [],
|
|
|
|
file_system_edits: [
|
|
|
|
CreateFile {
|
|
|
|
anchor: FileId(
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
dst: "foo.rs",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
is_snippet: false,
|
2020-05-22 16:03:08 +00:00
|
|
|
},
|
2020-08-11 14:13:40 +00:00
|
|
|
fix_trigger_range: 0..8,
|
|
|
|
},
|
2020-07-09 12:33:03 +00:00
|
|
|
),
|
2019-11-15 09:56:24 +00:00
|
|
|
},
|
2020-07-09 12:33:03 +00:00
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
2019-03-25 11:28:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 11:06:02 +00:00
|
|
|
#[test]
|
|
|
|
fn range_mapping_out_of_macros() {
|
2020-07-09 12:33:03 +00:00
|
|
|
// FIXME: this is very wrong, but somewhat tricky to fix.
|
|
|
|
check_fix(
|
|
|
|
r#"
|
|
|
|
fn some() {}
|
|
|
|
fn items() {}
|
|
|
|
fn here() {}
|
2020-04-17 11:06:02 +00:00
|
|
|
|
2020-07-09 12:33:03 +00:00
|
|
|
macro_rules! id { ($($tt:tt)*) => { $($tt)*}; }
|
2020-04-17 11:06:02 +00:00
|
|
|
|
2020-07-09 12:33:03 +00:00
|
|
|
fn main() {
|
|
|
|
let _x = id![Foo { a: <|>42 }];
|
|
|
|
}
|
2020-04-17 11:06:02 +00:00
|
|
|
|
2020-07-09 12:33:03 +00:00
|
|
|
pub struct Foo { pub a: i32, pub b: i32 }
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn {a:42, b: ()} {}
|
|
|
|
fn items() {}
|
|
|
|
fn here() {}
|
|
|
|
|
|
|
|
macro_rules! id { ($($tt:tt)*) => { $($tt)*}; }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let _x = id![Foo { a: 42 }];
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Foo { pub a: i32, pub b: i32 }
|
|
|
|
"#,
|
2020-04-17 11:06:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-21 16:05:15 +00:00
|
|
|
#[test]
|
|
|
|
fn test_check_unnecessary_braces_in_use_statement() {
|
2020-07-09 12:33:03 +00:00
|
|
|
check_no_diagnostics(
|
|
|
|
r#"
|
|
|
|
use a;
|
|
|
|
use a::{c, d::e};
|
|
|
|
"#,
|
2019-03-21 16:05:15 +00:00
|
|
|
);
|
2020-07-09 12:33:03 +00:00
|
|
|
check_fix(r#"use {<|>b};"#, r#"use b;"#);
|
|
|
|
check_fix(r#"use {b<|>};"#, r#"use b;"#);
|
|
|
|
check_fix(r#"use a::{c<|>};"#, r#"use a::c;"#);
|
|
|
|
check_fix(r#"use a::{self<|>};"#, r#"use a;"#);
|
|
|
|
check_fix(r#"use a::{c, d::{e<|>}};"#, r#"use a::{c, d::e};"#);
|
2019-03-21 16:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_check_struct_shorthand_initialization() {
|
2020-07-09 12:33:03 +00:00
|
|
|
check_no_diagnostics(
|
2019-03-21 16:05:15 +00:00
|
|
|
r#"
|
2020-07-09 12:33:03 +00:00
|
|
|
struct A { a: &'static str }
|
|
|
|
fn main() { A { a: "hello" } }
|
|
|
|
"#,
|
2019-03-21 16:05:15 +00:00
|
|
|
);
|
2020-07-09 12:33:03 +00:00
|
|
|
check_no_diagnostics(
|
2020-06-12 05:11:54 +00:00
|
|
|
r#"
|
2020-07-09 12:33:03 +00:00
|
|
|
struct A(usize);
|
|
|
|
fn main() { A { 0: 0 } }
|
|
|
|
"#,
|
2020-06-12 05:11:54 +00:00
|
|
|
);
|
2019-03-21 16:05:15 +00:00
|
|
|
|
2020-07-09 12:33:03 +00:00
|
|
|
check_fix(
|
2019-03-21 16:05:15 +00:00
|
|
|
r#"
|
2020-07-09 12:33:03 +00:00
|
|
|
struct A { a: &'static str }
|
2019-03-21 16:05:15 +00:00
|
|
|
fn main() {
|
|
|
|
let a = "haha";
|
2020-07-09 12:33:03 +00:00
|
|
|
A { a<|>: a }
|
2019-03-21 16:05:15 +00:00
|
|
|
}
|
2020-07-09 12:33:03 +00:00
|
|
|
"#,
|
2019-03-21 16:05:15 +00:00
|
|
|
r#"
|
2020-07-09 12:33:03 +00:00
|
|
|
struct A { a: &'static str }
|
2019-03-21 16:05:15 +00:00
|
|
|
fn main() {
|
|
|
|
let a = "haha";
|
2020-07-09 12:33:03 +00:00
|
|
|
A { a }
|
2019-03-21 16:05:15 +00:00
|
|
|
}
|
2020-07-09 12:33:03 +00:00
|
|
|
"#,
|
2019-03-21 16:05:15 +00:00
|
|
|
);
|
|
|
|
|
2020-07-09 12:33:03 +00:00
|
|
|
check_fix(
|
2019-03-21 16:05:15 +00:00
|
|
|
r#"
|
2020-07-09 12:33:03 +00:00
|
|
|
struct A { a: &'static str, b: &'static str }
|
2019-03-21 16:05:15 +00:00
|
|
|
fn main() {
|
|
|
|
let a = "haha";
|
|
|
|
let b = "bb";
|
2020-07-09 12:33:03 +00:00
|
|
|
A { a<|>: a, b }
|
2019-03-21 16:05:15 +00:00
|
|
|
}
|
2020-07-09 12:33:03 +00:00
|
|
|
"#,
|
2019-03-21 16:05:15 +00:00
|
|
|
r#"
|
2020-07-09 12:33:03 +00:00
|
|
|
struct A { a: &'static str, b: &'static str }
|
2019-03-21 16:05:15 +00:00
|
|
|
fn main() {
|
|
|
|
let a = "haha";
|
|
|
|
let b = "bb";
|
2020-07-09 12:33:03 +00:00
|
|
|
A { a, b }
|
2019-03-21 16:05:15 +00:00
|
|
|
}
|
2020-07-09 12:33:03 +00:00
|
|
|
"#,
|
2019-03-21 16:05:15 +00:00
|
|
|
);
|
|
|
|
}
|
2020-06-09 21:11:16 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_field_from_usage() {
|
2020-07-09 11:59:49 +00:00
|
|
|
check_fix(
|
2020-06-09 21:11:16 +00:00
|
|
|
r"
|
2020-06-24 09:05:47 +00:00
|
|
|
fn main() {
|
2020-07-09 11:59:49 +00:00
|
|
|
Foo { bar: 3, baz<|>: false};
|
2020-06-24 09:05:47 +00:00
|
|
|
}
|
|
|
|
struct Foo {
|
|
|
|
bar: i32
|
|
|
|
}
|
|
|
|
",
|
2020-06-09 21:11:16 +00:00
|
|
|
r"
|
2020-06-24 09:05:47 +00:00
|
|
|
fn main() {
|
|
|
|
Foo { bar: 3, baz: false};
|
|
|
|
}
|
|
|
|
struct Foo {
|
|
|
|
bar: i32,
|
|
|
|
baz: bool
|
|
|
|
}
|
|
|
|
",
|
2020-06-09 21:11:16 +00:00
|
|
|
)
|
|
|
|
}
|
2020-06-22 19:29:11 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_field_in_other_file_from_usage() {
|
|
|
|
check_apply_diagnostic_fix_in_other_file(
|
|
|
|
r"
|
|
|
|
//- /main.rs
|
|
|
|
mod foo;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
<|>foo::Foo { bar: 3, baz: false};
|
|
|
|
}
|
|
|
|
//- /foo.rs
|
|
|
|
struct Foo {
|
|
|
|
bar: i32
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct Foo {
|
|
|
|
bar: i32,
|
|
|
|
pub(crate) baz: bool
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
2019-03-21 16:05:15 +00:00
|
|
|
}
|