Skip to content

Commit 45ac2b2

Browse files
committed
Code style adjustments
1 parent cfbee8d commit 45ac2b2

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

crates/hir_ty/src/diagnostics/decl_check.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use hir_expand::{
1919
};
2020
use syntax::{
2121
ast::{self, NameOwner},
22-
AstPtr,
22+
AstNode, AstPtr,
2323
};
2424

2525
use crate::{
@@ -259,6 +259,21 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
259259
if let Some(expr) = source_ptr.value.as_ref().left() {
260260
let root = source_ptr.file_syntax(db.upcast());
261261
if let ast::Pat::IdentPat(ident_pat) = expr.to_node(&root) {
262+
let parent = match ident_pat.syntax().parent() {
263+
Some(parent) => parent,
264+
None => continue,
265+
};
266+
267+
// We have to check that it's either `let var = ...` or `Variant(_) @ var` statement,
268+
// because e.g. match arms are patterns as well.
269+
// In other words, we check that it's a named variable binding.
270+
if !ast::LetStmt::cast(parent.clone()).is_some()
271+
&& !ast::IdentPat::cast(parent).is_some()
272+
{
273+
// This pattern is not an actual variable declaration, e.g. `Some(val) => {..}` match arm.
274+
continue;
275+
}
276+
262277
let diagnostic = IncorrectCase {
263278
file: source_ptr.file_id,
264279
ident_type: "Variable".to_string(),
@@ -663,7 +678,7 @@ fn foo2(ok_param: &str, CAPS_PARAM: u8) {}
663678
r#"
664679
fn foo() {
665680
let SOME_VALUE = 10;
666-
// ^^^^^^^^^^ Variable `SOME_VALUE` should have a snake_case name, e.g. `some_value`
681+
// ^^^^^^^^^^ Variable `SOME_VALUE` should have snake_case name, e.g. `some_value`
667682
let AnotherValue = 20;
668683
// ^^^^^^^^^^^^ Variable `AnotherValue` should have snake_case name, e.g. `another_value`
669684
}
@@ -758,6 +773,39 @@ impl someStruct {
758773
// ^^^^^^^^^^^^^^^ Variable `WHY_VAR_IS_CAPS` should have snake_case name, e.g. `why_var_is_caps`
759774
}
760775
}
776+
"#,
777+
);
778+
}
779+
780+
#[test]
781+
fn no_diagnostic_for_enum_varinats() {
782+
check_diagnostics(
783+
r#"
784+
enum Option { Some, None }
785+
786+
fn main() {
787+
match Option::None {
788+
None => (),
789+
Some => (),
790+
}
791+
}
792+
"#,
793+
);
794+
}
795+
796+
#[test]
797+
fn non_let_bind() {
798+
check_diagnostics(
799+
r#"
800+
enum Option { Some, None }
801+
802+
fn main() {
803+
match Option::None {
804+
None @ SOME_VAR => (),
805+
// ^^^^^^^^ Variable `SOME_VAR` should have snake_case name, e.g. `some_var`
806+
Some => (),
807+
}
808+
}
761809
"#,
762810
);
763811
}

crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! Functions for string case manipulation, such as detecting the identifier case,
2+
//! and converting it into appropriate form.
3+
14
#[derive(Debug)]
25
enum DetectedCase {
36
LowerCamelCase,
@@ -44,6 +47,8 @@ fn detect_case(ident: &str) -> DetectedCase {
4447
}
4548
}
4649

50+
/// Converts an identifier to an UpperCamelCase form.
51+
/// Returns `None` if the string is already is UpperCamelCase.
4752
pub fn to_camel_case(ident: &str) -> Option<String> {
4853
let detected_case = detect_case(ident);
4954

@@ -87,9 +92,17 @@ pub fn to_camel_case(ident: &str) -> Option<String> {
8792
}
8893
}
8994

90-
Some(output)
95+
if output == ident {
96+
// While we didn't detect the correct case at the beginning, there
97+
// may be special cases: e.g. `A` is both valid CamelCase and UPPER_SNAKE_CASE.
98+
None
99+
} else {
100+
Some(output)
101+
}
91102
}
92103

104+
/// Converts an identifier to a lower_snake_case form.
105+
/// Returns `None` if the string is already is lower_snake_case.
93106
pub fn to_lower_snake_case(ident: &str) -> Option<String> {
94107
// First, assume that it's UPPER_SNAKE_CASE.
95108
match detect_case(ident) {
@@ -102,9 +115,18 @@ pub fn to_lower_snake_case(ident: &str) -> Option<String> {
102115

103116
// Otherwise, assume that it's CamelCase.
104117
let lower_snake_case = stdx::to_lower_snake_case(ident);
105-
Some(lower_snake_case)
118+
119+
if lower_snake_case == ident {
120+
// While we didn't detect the correct case at the beginning, there
121+
// may be special cases: e.g. `a` is both valid camelCase and snake_case.
122+
None
123+
} else {
124+
Some(lower_snake_case)
125+
}
106126
}
107127

128+
/// Converts an identifier to an UPPER_SNAKE_CASE form.
129+
/// Returns `None` if the string is already is UPPER_SNAKE_CASE.
108130
pub fn to_upper_snake_case(ident: &str) -> Option<String> {
109131
match detect_case(ident) {
110132
DetectedCase::UpperSnakeCase => return None,
@@ -117,7 +139,14 @@ pub fn to_upper_snake_case(ident: &str) -> Option<String> {
117139
// Normalize the string from whatever form it's in currently, and then just make it uppercase.
118140
let upper_snake_case =
119141
stdx::to_lower_snake_case(ident).chars().map(|c| c.to_ascii_uppercase()).collect();
120-
Some(upper_snake_case)
142+
143+
if upper_snake_case == ident {
144+
// While we didn't detect the correct case at the beginning, there
145+
// may be special cases: e.g. `A` is both valid CamelCase and UPPER_SNAKE_CASE.
146+
None
147+
} else {
148+
Some(upper_snake_case)
149+
}
121150
}
122151

123152
#[cfg(test)]
@@ -139,6 +168,7 @@ mod tests {
139168
check(to_lower_snake_case, "Weird_Case", expect![["weird_case"]]);
140169
check(to_lower_snake_case, "CamelCase", expect![["camel_case"]]);
141170
check(to_lower_snake_case, "lowerCamelCase", expect![["lower_camel_case"]]);
171+
check(to_lower_snake_case, "a", expect![[""]]);
142172
}
143173

144174
#[test]
@@ -151,6 +181,7 @@ mod tests {
151181
check(to_camel_case, "UPPER_SNAKE_CASE", expect![["UpperSnakeCase"]]);
152182
check(to_camel_case, "Weird_Case", expect![["WeirdCase"]]);
153183
check(to_camel_case, "name", expect![["Name"]]);
184+
check(to_camel_case, "A", expect![[""]]);
154185
}
155186

156187
#[test]
@@ -160,5 +191,6 @@ mod tests {
160191
check(to_upper_snake_case, "Weird_Case", expect![["WEIRD_CASE"]]);
161192
check(to_upper_snake_case, "CamelCase", expect![["CAMEL_CASE"]]);
162193
check(to_upper_snake_case, "lowerCamelCase", expect![["LOWER_CAMEL_CASE"]]);
194+
check(to_upper_snake_case, "A", expect![[""]]);
163195
}
164196
}

crates/hir_ty/src/diagnostics/unsafe_check.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,13 @@ struct Ty {
190190
a: u8,
191191
}
192192
193-
static mut static_mut: Ty = Ty { a: 0 };
193+
static mut STATIC_MUT: Ty = Ty { a: 0 };
194194
195195
fn main() {
196-
let x = static_mut.a;
196+
let x = STATIC_MUT.a;
197197
//^^^^^^^^^^ This operation is unsafe and requires an unsafe function or block
198198
unsafe {
199-
let x = static_mut.a;
199+
let x = STATIC_MUT.a;
200200
}
201201
}
202202
"#,

0 commit comments

Comments
 (0)