2021-04-09 12:39:07 +00:00
|
|
|
//! Type inference-based diagnostics.
|
2020-07-14 08:52:18 +00:00
|
|
|
mod expr;
|
2020-07-14 08:18:08 +00:00
|
|
|
mod match_check;
|
2020-07-14 08:52:18 +00:00
|
|
|
mod unsafe_check;
|
2020-10-03 09:48:02 +00:00
|
|
|
mod decl_check;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2021-06-12 19:05:23 +00:00
|
|
|
use std::fmt;
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2020-12-17 00:19:56 +00:00
|
|
|
use base_db::CrateId;
|
2021-06-12 16:28:19 +00:00
|
|
|
use hir_def::ModuleDefId;
|
2021-06-12 19:05:23 +00:00
|
|
|
use hir_expand::HirFileId;
|
|
|
|
use syntax::{ast, AstPtr};
|
2019-11-27 14:46:02 +00:00
|
|
|
|
2021-06-12 19:05:23 +00:00
|
|
|
use crate::db::HirDatabase;
|
2020-07-14 08:28:55 +00:00
|
|
|
|
2021-06-12 14:39:46 +00:00
|
|
|
pub use crate::diagnostics::{
|
2021-06-12 16:28:19 +00:00
|
|
|
expr::{
|
|
|
|
record_literal_missing_fields, record_pattern_missing_fields, BodyValidationDiagnostic,
|
|
|
|
},
|
2021-06-12 14:39:46 +00:00
|
|
|
unsafe_check::missing_unsafe,
|
|
|
|
};
|
2020-07-14 08:52:18 +00:00
|
|
|
|
2020-10-03 09:48:02 +00:00
|
|
|
pub fn validate_module_item(
|
|
|
|
db: &dyn HirDatabase,
|
2020-12-17 00:19:56 +00:00
|
|
|
krate: CrateId,
|
2020-10-03 09:48:02 +00:00
|
|
|
owner: ModuleDefId,
|
2021-06-12 19:05:23 +00:00
|
|
|
) -> Vec<IncorrectCase> {
|
2020-10-04 06:26:39 +00:00
|
|
|
let _p = profile::span("validate_module_item");
|
2021-06-12 19:05:23 +00:00
|
|
|
let mut validator = decl_check::DeclValidator::new(db, krate);
|
2020-12-17 00:19:56 +00:00
|
|
|
validator.validate_item(owner);
|
2021-06-12 19:05:23 +00:00
|
|
|
validator.sink
|
2020-10-03 09:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum CaseType {
|
|
|
|
// `some_var`
|
|
|
|
LowerSnakeCase,
|
|
|
|
// `SOME_CONST`
|
|
|
|
UpperSnakeCase,
|
|
|
|
// `SomeStruct`
|
|
|
|
UpperCamelCase,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for CaseType {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let repr = match self {
|
|
|
|
CaseType::LowerSnakeCase => "snake_case",
|
|
|
|
CaseType::UpperSnakeCase => "UPPER_SNAKE_CASE",
|
2020-10-03 11:47:46 +00:00
|
|
|
CaseType::UpperCamelCase => "CamelCase",
|
2020-10-03 09:48:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
write!(f, "{}", repr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 15:09:45 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum IdentType {
|
|
|
|
Constant,
|
|
|
|
Enum,
|
|
|
|
Field,
|
|
|
|
Function,
|
2021-05-31 16:09:44 +00:00
|
|
|
Parameter,
|
2021-02-05 15:09:45 +00:00
|
|
|
StaticVariable,
|
|
|
|
Structure,
|
|
|
|
Variable,
|
|
|
|
Variant,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for IdentType {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let repr = match self {
|
|
|
|
IdentType::Constant => "Constant",
|
|
|
|
IdentType::Enum => "Enum",
|
|
|
|
IdentType::Field => "Field",
|
|
|
|
IdentType::Function => "Function",
|
2021-05-31 16:09:44 +00:00
|
|
|
IdentType::Parameter => "Parameter",
|
2021-02-05 15:09:45 +00:00
|
|
|
IdentType::StaticVariable => "Static variable",
|
|
|
|
IdentType::Structure => "Structure",
|
|
|
|
IdentType::Variable => "Variable",
|
|
|
|
IdentType::Variant => "Variant",
|
|
|
|
};
|
|
|
|
|
|
|
|
write!(f, "{}", repr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 09:48:02 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct IncorrectCase {
|
|
|
|
pub file: HirFileId,
|
2020-10-08 06:27:38 +00:00
|
|
|
pub ident: AstPtr<ast::Name>,
|
2020-10-03 09:48:02 +00:00
|
|
|
pub expected_case: CaseType,
|
2021-02-05 15:09:45 +00:00
|
|
|
pub ident_type: IdentType,
|
2020-10-03 09:48:02 +00:00
|
|
|
pub ident_text: String,
|
|
|
|
pub suggested_text: String,
|
|
|
|
}
|